next-intl icon indicating copy to clipboard operation
next-intl copied to clipboard

Automatically prefer localized pathnames that are more specific

Open Hdanzo opened this issue 11 months ago • 1 comments

Description

When using localized pathnames and trying to access a page (/products/add) that is in the same folder as a dynamic page (/products/[productId]), the user is redirected to the dynamic page with the name of the static page as the param (productId = "add").

This however is fixable by ordering "/products/add" before "/products/[productId]" in the pathnames object.

If this behavior is the expected one and not something that's planned to be changed, I suggest this behavior to be stated in the documentation.

Mandatory reproduction URL

https://codesandbox.io/p/devbox/xenodochial-hodgkin-3wj7ll?file=%2Fapp%2F%5Blocale%5D%2Fpage.tsx%3A94%2C11

Reproduction description

Steps to reproduce:

  1. Open reproduction.
  2. Change locale to "es" in the URL.
  3. Click on "Agregar" Link.
  4. Get redirected to "/productos/[productId]" instead of "/productos/agregar"

Expected behaviour

  1. Open reproduction.
  2. Change locale to "es" in the URL.
  3. Click on "Agregar" Link.
  4. Get redirected to "/productos/agregar" instead of "/productos/[productId]"

Hdanzo avatar Mar 05 '24 15:03 Hdanzo

Thanks for this report, that's a great point!

I wonder if next-intl could automatically do the right thing here, Next.js is certainly capable of that.

This would need some further research on how routes can be ordered in a meaningful way. If I'm not mistaken, this is how Next.js does it: sorted-routes.ts. There's another related note in normalize-catchall-routes.ts.

And this is where we need to apply it in next-intl:

https://github.com/amannn/next-intl/blob/419a9cea2817949b65b1af9724927b95d7d3bf01/packages/next-intl/src/middleware/utils.tsx#L21

I'll add a note about this to the docs for the time being but would be open to reviewing a PR here in case someone is interested in diving deeper into this.

amannn avatar Mar 06 '24 11:03 amannn

@amannn I could take a look at this when I get the chance and open a PR if no one is already working on it 🙂.

fkapsahili avatar Mar 30 '24 14:03 fkapsahili

@fkapsahili Absolutely if you're interested, always a pleasure! 🙌

amannn avatar Mar 30 '24 17:03 amannn