Based on similar issues, I decided to investigate why I encountered the same problem with the global-error.tsx file when the root layout.tsx thrown an error.
Initially, I suspected that the i18n library (next-intl) I used in my main project was the cause. However, I later realized that the problem lies in dynamic routes.
Why?
Many users, including myself, find it unintuitive from the official documentation about i18n and the example on github.com that the global-error.tsx file should be placed in the root of the app/ directory instead of app/[lang]/. The documentation and example do not cover this detail, which leads users to place global-error.tsx in app/[lang]/. As a result, when global-error.tsxcannot catch errors in root layout.tsx, users report this as a new issue on Next.js's GitHub, as seen in the related issues linked above.
How?
My bug fix allows the global-error.tsx file to be placed not only in the root of app/ but also in app/[lang]/. This fix eliminates the need to update the documentation to specify where the global-error.tsx file should be placed or to change the example on github.com to clarify this point.
p.s. you can use any dynamic route for your i18n Next.js applications; lang is just an example as shown in the official documentation.
What?
Based on similar issues, I decided to investigate why I encountered the same problem with the
global-error.tsx
file when the rootlayout.tsx
thrown an error.Related issues: https://github.com/vercel/next.js/issues/61400 https://github.com/vercel/next.js/issues/60245
Initially, I suspected that the i18n library (next-intl) I used in my main project was the cause. However, I later realized that the problem lies in dynamic routes.
Why?
Many users, including myself, find it unintuitive from the official documentation about i18n and the example on github.com that the
global-error.tsx
file should be placed in the root of theapp/
directory instead ofapp/[lang]/
. The documentation and example do not cover this detail, which leads users to placeglobal-error.tsx
inapp/[lang]/
. As a result, whenglobal-error.tsx
cannot catch errors in rootlayout.tsx
, users report this as a new issue on Next.js's GitHub, as seen in the related issues linked above.How?
My bug fix allows the
global-error.tsx
file to be placed not only in the root ofapp/
but also inapp/[lang]/
. This fix eliminates the need to update the documentation to specify where theglobal-error.tsx
file should be placed or to change the example on github.com to clarify this point.p.s. you can use any dynamic route for your i18n Next.js applications;
lang
is just an example as shown in the official documentation.Fixes #66905