-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
feat: Automatically prefer localized pathnames that are more specific #983
Merged
amannn
merged 25 commits into
amannn:main
from
fkapsahili:fix/automatically-prefer-localized-pathnames
Apr 26, 2024
Merged
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2550e48
Ensure more specific localized pathnames are preferred
fkapsahili 8fa7d7e
Make sure that static pathnames are prioritized over optional catch-a…
fkapsahili 012de6c
Update docs to reflect changes in route prioritization
fkapsahili 1837b98
Increase middleware size limit
fkapsahili 752f01e
Add playwright tests
fkapsahili 3d4d3a6
Update middleware docs
fkapsahili cd438a9
Make sure prioritization works for nested dynamic routes
fkapsahili 8dd2573
Merge branch 'main' into fix/automatically-prefer-localized-pathnames
fkapsahili 40c994a
Increase middleware size limit
fkapsahili bc659c9
Merge branch 'fix/automatically-prefer-localized-pathnames' of https:…
fkapsahili f7a8bfb
Increase middleware size limit
fkapsahili d13fcf3
Merge branch 'main' into fix/automatically-prefer-localized-pathnames
fkapsahili 0efe8a2
Increase middleware size limit
fkapsahili 1f00897
Update packages/next-intl/src/middleware/utils.tsx
fkapsahili effc0e6
Update packages/next-intl/src/middleware/utils.tsx
fkapsahili 961c61f
Update packages/next-intl/test/middleware/middleware.test.tsx
fkapsahili 9e9fd75
Update packages/next-intl/test/middleware/middleware.test.tsx
fkapsahili d983757
Update packages/next-intl/test/middleware/middleware.test.tsx
fkapsahili 2708b17
Update packages/next-intl/test/middleware/middleware.test.tsx
fkapsahili 686882a
Merge branch 'main' into fix/automatically-prefer-localized-pathnames
fkapsahili bad1728
Update packages/next-intl/test/middleware/utils.test.tsx
fkapsahili f73628a
Fix misspellings
fkapsahili 3f3acb4
Simplify pathname sorting function
fkapsahili 59561c2
Fix misspellings in playwright tests
fkapsahili 635a4bf
Rename new functions to indicate we're operating on segments (e.g. `i…
amannn File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
examples/example-app-router-playground/src/app/[locale]/news/just-in/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {useTranslations} from 'next-intl'; | ||
|
||
export default function NewsArticle() { | ||
const t = useTranslations('JustIn'); | ||
return <h1>{t('title')}</h1>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,7 +138,7 @@ | |
}, | ||
{ | ||
"path": "dist/production/middleware.js", | ||
"limit": "5.95 KB" | ||
"limit": "6.10 KB" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diving a bit deeper into this, I'm not sure these flags are sufficient to be honest.
E.g. consider this case:
In this case, the routes would be considered equal in regard to priority, and probably the natural ordering would be preserved. However, the second route is more specific and should match.
The number of dynamic segments could be another indicator, but I think this won't solve the problem cleanly as e.g.
/foo/[d1]
and/foo/bar/[d1]
have the same number of segments.It seems like to be able to tell which route is really more specific, we have to consider routes in a hierarchical way, e.g. find the most specific route in
/articles
, then try to find the most specific route in./[category]
, etc.Do you by chance know if Next.js handles these cases correctly? Maybe we can take some inspiration from them.
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe the number of dynamic segments could in fact suffice? 🤔
In the example
/foo/[d1]
and/foo/bar/[d1]
, the routes have the same priority, but only one of them matches eventually—so maybe this isn't an issue.Do you think you could a few more tests for edge cases so we can gain some clarity about those?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, thanks for the hint! You were absolutely right, my solution didn't take into account the case where we have nested dynamic routes, e.g:
/articles/[category]/[slug]
and a static route in the same place 😅.According to my tests, Next.js handles these cases correctly but I'm not very familiar with the Next.js repo itself, but at first glance I believe the code you referenced in the corresponding issue is actually where Next does the route sorting:
https://github.com/vercel/next.js/blob/415cd74b9a220b6f50da64da68c13043e9b02995/packages/next/src/shared/lib/router/utils/sorted-routes.ts
When looking at Next's code I realized that it is necessary that the individual route segments are compared with each other and have now implemented this additionally so that the failed tests that I added at the beginning of this update now pass.
If there are any other edge cases you can think of, I'm happy to add more tests! 🙂