vercel / vercel/next.js

Next internationalized routing allows multiple locales in url path

Open
#31,420 8 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Internationalization (i18n)
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:3000
  • http://localhost:3000/en
  • http://localhost:3000/en/en
  • http://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
  1. Setup a test repository using yarn create next-app --typescript
  2. Add the following i18n config to the next.config.js: i18n: { defaultLocale: "en", locales: ["en", "de", "us"] },
  3. Start the development server using yarn dev
  4. Visit http://localhost:3000/en/en, http://localhost:3000/en, http://localhost:3000 and see the exact same page
  5. Add a [slug] to the pages folder and create an index file in the [slug] folder
  6. You can access the page using http://localhost:3000/en/test but not using http://localhost:3000/en/en, http://localhost:3000/en/de or http://localhost:3000/en/us
  7. You can also acces the page using e.g. http://localhost:3000/en/en/test which then redirects to just http://localhost:3000/en/test

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.