8000 Link-component drops query string when localePrefix is set to as-needed · Issue #1731 · amannn/next-intl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Link-component drops query string when localePrefix is set to as-needed #1731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
seanpavlov opened this issue Feb 21, 2025 · 4 comments · Fixed by #1732
Closed
3 tasks done

Link-component drops query string when localePrefix is set to as-needed #1731

seanpavlov opened this issue Feb 21, 2025 · 4 comments · Fixed by #1732
Labels
bug Something isn't working unconfirmed Needs triage.

Comments

@seanpavlov
Copy link

Description

Query string is not present in the generated href from next-intl's Link component.
When localePrefix is set to as-needed, the query string is not present for default locale, but is present for other locales (which has the locale prefix).

Example:

  • Setup
    • Default locale set to en and sv available as another locale
    • localePrefix set to as-needed
<Link href={{ pathname: "/foo", query: { msg: "bar" } }} locale="en">Test</Link>
// Generates `localhost:3000/foo`

<Link href={{ pathname: "/foo", query: { msg: "bar" } }} locale="sv">Test</Link>
// Generates `localhost:3000/sv/foo?msg=bar`

The minimal reproduction covers the bug, but I thought that I could highlight how our setup looks like since it differs a little bit:

  • We have 4 different domains
    • Each domain has a different default locale (for instance domain.se has sv as default locale, domain.no has no as default locale)
    • One of the domains has a lot of locales - domain.com has en as default locale, but also es and other locales
  • localePrefix is set to as-needed as we want domain.se to point to sv

Verifications

Mandatory reproduction URL

https://github.com/seanpavlov/reproduction-next-intl-missing-query-string

Reproduction description

Getting started

  1. Install pnpm
  2. Run pnpm install in root
  3. Run pnpm dev

How to reproduce the bug

  1. On the page: Click on Swedish-link
  2. You will now see that the route has changed to localhost:3000/sv
  3. Hover over A link to another page with query string-link. You will now see that the page has ?name=guest as query string appended to the url
  4. Click on English-link
  5. You will now see that the route has changed to localhost:3000
  6. Hover over A link to another page with query string-link. You will now see that the page doesn't have any query string appended to the url

Expected behaviour

Expected behavior

Expected behavior is that both the default locale and the other locales generates a href with the query string included:

<Link href={{ pathname: "/foo", query: { msg: "bar" } }} locale="en">Test</Link>
// Generates `localhost:3000/foo?msg=bar`

<Link href={{ pathname: "/foo", query: { msg: "bar" } }} locale="sv">Test</Link>
// Generates `localhost:3000/sv/foo?msg=bar`

Possible solution

By passing query: href.query here I managed to get the expected behavior.

Could that be a solution or would it have other implications?

@seanpavlov seanpavlov added bug Something isn't working unconfirmed Needs triage. labels Feb 21, 2025
@amannn
Copy link
Owner
amannn commented Feb 21, 2025

Thank you so much for the careful reproduction and for even looking into where to fix this!

I can reproduce the bug in your repo, let me have a look.

amannn added a commit that referenced this issue Feb 21, 2025
… 'as-needed'` with `domains` (#1732)

Fixes #1731 

Many thanks to @seanpavlov for analyzing the issue and suggesting the
fix!
@amannn
Copy link
Owner
amannn commented Feb 21, 2025

Out in v3.26.5, many thanks for your help! 🙌

@seanpavlov
Copy link
Author

Wow, that was fast!
Verified on our end and it works as expected.

Thanks a lot for the help 🙏 Really appreciate it!

@amannn
Copy link
Owner
amannn commented Feb 21, 2025

Sure, glad to help!

As a side note, while looking into the issue you discovered, I think I found a chance for a great improvement for the localePrefix: 'as-needed' & domains case: #1733. This combination was always bugging me and required special care, but I think I finally have an idea for a clean solution for this use case.

Since you're using this combination, would you like to have a look and provide feedback? next-intl@4 is currently in beta and it would offer a chance to get the required breaking change in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed Needs triage.
Projects
None yet
2 participants
0