Next internationalized routing allows multiple locales in url path
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 142k
- Forks
- 32.4k
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 351
Description
What version of Next.js are you using?
11.0.1 and 12.0.0
What version of Node.js are you using?
14.18.1
What browser are you using?
Firefox, Chrome
What operating system are you using?
macOS
How are you deploying your application?
next dev
Describe the Bug
When adding internationalized routing to a Next.js app, multiple subpaths redirect to the same route.
If for example I use the "en" locale as the defaultLocale, the following urls route to the same index page:
http://localhost:3000http://localhost:3000/enhttp://localhost:3000/en/enhttp://localhost:3000/[anyOtherLocaleInNextConfig]/en
The router object for the http://localhost:3000/en/en option looks similar to this:
{ "pathname": "/", "route": "/", "query": {}, "asPath": "/en", "components": { "/": { "initial": true, "props": { "pageProps": {} } }, "/_app": { "styleSheets": [] } }, "isFallback": false, "basePath": "", "locale": "en", "locales": [ "de", "en", "us" ], "defaultLocale": "en", "isReady": true, "isPreview": false, "isLocaleDomain": false, "events": {} }
This causes problems when using slugs like http://localhost:3000/locale/[:slug] because the second /en in the http://localhost:3000/en/en url doesn't get detected as a slug. However using any string other than a locale defined in the next.config.js works just fine.
I hope this description is comprehensible - I've been banging my head against my desk for the past few days and might have lost one or two braincells.
Expected Behavior
When using slugs like http://localhost:3000/locale/[:slug], the second /en in localhost:3000/en/en should be detected as the slug.
To Reproduce
- Setup a test repository using
yarn create next-app --typescript - Add the following i18n config to the
next.config.js:i18n: { defaultLocale: "en", locales: ["en", "de", "us"] }, - Start the development server using
yarn dev - Visit
http://localhost:3000/en/en,http://localhost:3000/en,http://localhost:3000and see the exact same page - Add a
[slug]to the pages folder and create an index file in the[slug]folder - You can access the page using
http://localhost:3000/en/testbut not usinghttp://localhost:3000/en/en,http://localhost:3000/en/deorhttp://localhost:3000/en/us - You can also acces the page using e.g.
http://localhost:3000/en/en/testwhich then redirects to justhttp://localhost:3000/en/test
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the behavior with the i18n settings in next.config.js, then add the [slug] page under pages and run the app with yarn dev. Verify that /en/en treats the second en as the slug without redirecting, while the other documented locale routes continue to behave as expected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nextjs, node.js
- Domain
- frontend, internationalization, web-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100