next.js
next.js copied to clipboard
I18n configuration in next.config.js breaks app directory
Verify canary release
- [X] I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 22.6.0: Wed Jul 5 22:21:56 PDT 2023; root:xnu-8796.141.3~6/RELEASE_X86_64
Binaries:
Node: 20.5.0
npm: 9.8.0
Yarn: 3.5.0
pnpm: 8.6.12
Relevant Packages:
next: 13.4.14-canary.0
eslint-config-next: 13.4.13
react: 18.2.0
react-dom: 18.2.0
typescript: 5.1.6
Next.js Config:
output: N/A
Which area(s) of Next.js are affected? (leave empty if unsure)
App Router, Internationalization (i18n), Routing (next/router, next/navigation, next/link)
Link to the code that reproduces this issue or a replay of the bug
https://github.com/fabio-nettis/next-i18n-error
To Reproduce
This issue is directly related to my previous issue #53665 You can find more about the discovery process of this bug in this discussion.
- Clone the repository
- Try to navigate to any of the defined pages
- You will encounter a not found error for each of the defined routes.
Once you have seen the error you can then do the following steps to revert the broken app folder:
- Remove
i18nkey from next config - Try to navigate to any of the defined pages
- See the defined page
Describe the Bug
Since version 13.4.13-canary.0 an application previously working fine on version 13.4.12 returns a 404 status code for every page in the app folder when the i18n setting is set in the next config file.
Expected Behavior
Setting the i18n in the next config file should not break the whole application' routing.
Which browser are you using? (if relevant)
Chrome 114.0.5735.106 (Official Build) (x86_64)
How are you deploying your application? (if relevant)
next dev
For anybody that needs a solution now, here is a repository with a working and scalable i18n implementation that does not use the i18n settings inside next.config.js.
For anybody that needs a solution now, here is a repository with a working and scalable i18n implementation that does not use the i18n settings inside
next.config.js.
Thanks, but it doesn't work in case of default locale shouldn't show in the url, e.g.: en (default) -> /blog/post de -> /de/blog/post
@vesurbag I will be tracking this issue here, currently optional route parameters are not supported, they only work for catch-all segments.
same issue. tested with [email protected]
The issue persists in v13.5.2.
As we have a lot of translated pages in Pages Router, this bug stops us from upgrading Next and adopting the App Router incrementally.
The moment I remove i18n config, the App Router works again, but then everything in Pages Router stops working.
The moment I remove i18n config, the App Router works again, but then everything in Pages Router stops working.
And does the app dir pages work in production? For me they only work in development.
And does the app dir pages work in production? For me they only work in development.
For us, they do, but we're stuck on v13.2.
same issue. tested with [email protected]
Check out the example repository I have provided, it eliminates the need for the i18n key inside next.config.js. We currently use it in production and it works just fine.
Check out the example repository I have provided, it eliminates the need for the i18n key inside next.config.js. We currently use it in production and it works just fine.
@fabio-nettis Appreciate trying to find a workaround. However, i18n config key is not needed for internationalization in App Router, it's meant to be used in the Pages Router. In fact, your example repository implements a standard approach suggested by Next.js docs: https://nextjs.org/docs/app/building-your-application/routing/internationalization
It's not a problem, of course ;-) However, that's not a workaround for this issue.
To sum up: Existence of i18n config key needed for internationalization in Pages Router breaks App Router, and there is currently no workaround that would enable one to use internationalization in Pages Router and App Router (in general) at the same time.
Check out the example repository I have provided, it eliminates the need for the i18n key inside next.config.js. We currently use it in production and it works just fine.
@fabio-nettis Appreciate trying to find a workaround. However,
i18nconfig key is not needed for internationalization in App Router, it's meant to be used in the Pages Router. In fact, your example repository implements a standard approach suggested by Next.js docs: https://nextjs.org/docs/app/building-your-application/routing/internationalizationIt's not a problem, of course ;-) However, that's not a workaround for this issue.
To sum up: Existence of
i18nconfig key needed for internationalization in Pages Router breaks App Router, and there is currently no workaround that would enable one to use internationalization in Pages Router and App Router (in general) at the same time.
@dmgawel Thanks for the response! it would probably be wise to explicitly mention the required absence of the i18n key in next.config.js for the app router, since this did not resonate to me personally, and based on the issue activity, suggestively neither did to other people.
Furthermore I'd politely recommend to extend the current documentation on localization inside the app router to be a bit closer to a real-world approach as seen in the repository I provided.
so to summarize the issue - the incremental migration to the app router is not possible for localized NextJs apps, right?
so to summarize the issue - the incremental migration to the app router is not possible for localized NextJs apps, right?
Right 👍
it looks that the locale is not being updated in getServerSideProps/getStaticProps from the next version 13.4.13, at least what this issue seems to suggest:
@dmgawel so to summarize the issue - the incremental migration to the app router is not possible for localized NextJs apps, right?
We are on next 13.5.4 and I can confirm this conclusion. Does anyone know a version of next where it was still working to use App and Pages router simultaneously with i18n?
@dmgawel so to summarize the issue - the incremental migration to the app router is not possible for localized NextJs apps, right?
We are on
next13.5.4and I can confirm this conclusion. Does anyone know a version ofnextwhere it was still working to use App and Pages router simultaneously with i18n?
On my side, I'm re-implementing the i18n feature with a middleware. The only downside is that I cannot remove nor disable the locale property on the next/router and next/link modules. Otherwise, even if I have to update all the links to include the locale in the href, it's not so complicated.
This way, the locale (from the route segment [locale]) is retrieved from the query params and it works like in App Router.
It seems to be resolved on the latest release. However, the ##46622 still persist
It seems to be resolved on the latest release
Tested on v13.5.6, unfortunately it still doesn't work.
It still doesn't work on v14. Has anyone found a workaround to resolve this issue?
For anybody that needs a solution now, here is a repository with a working and scalable i18n implementation that does not use the i18n settings inside
next.config.js.
@Abd2rahman Check the linked repository. The only thing this example does not support are optional locale paths.
Thank you @fabio-nettis, but the solution you are suggesting uses only the app router, and the bug occurs only when we combine both the app router and page router in the same project. Does it work as well with page router ?
I created a repository that recreates the issue with a minimal amount of code. It features both Pages and App router example, to highlight the issue when both are used:
https://github.com/dmgawel/next-i18n-issue
I created a repository that recreates the issue with a minimal amount of code. It features both Pages and App router example, to highlight the issue when both are used:
https://github.com/dmgawel/next-i18n-issue
It is same situation with mine🥲 Have you ever tried use middleware as Next.js docs said?
I think I almost find solution from https://github.com/amannn/next-intl/tree/main/examples/example-app-router-migration.
bump on the thread - what's the status of this issue? I followed the next docs for the middleware code and I'm hitting the exact same issue somewhere between sending a response and next trying to unravel the [lang] path param
@shinwonse "almost?"
@shinwonse "almost?"
I thought I almost solved it with next-intl package, but it was failed and I am still struggling with it 😂
I got it working using their official doc https://next-intl-docs.vercel.app/docs/getting-started/app-router. I've checked your repo, @shinwonse and it looks like you're almost there, you'd have to use the lang/locale param in the layout instead of using a hook and call the unstable_setRequestLocale() with that locale.
We're using a middleware to match domain names and paths, and then we either prepend to, or change the prefix of the path to the fully qualified locale.
This works well for the pages router, but as long as i18n is configured, it won't match app/[locale]/example/page.tsx, only app/example/page.tsx when visiting /example.
The middleware does indeed rewrite to /en-GB/example, but the app router seems to remove that part or just ignore the rewrite, when i18n is configured.
Edit:
In order to not be blocked by this issue, we ended up abandoning the use of i18n in next.config, and instead opted to handle it using NextResponse.rewrite in our middleware, and moving all pages routes to pages/[locale].
After moving all pages, we had to replace all uses of useRouter's locale, and AppContext's locale param.
This issue should be on docs, I just randomly spent 3 hours to remove the i18n config. 🫠
Any info on how to solve this problem with output:export? It works fine in local but not when I generate the static files.