vercel / next.js

The React Framework
https://nextjs.org
MIT License
122.51k stars 26.22k forks source link

fix: global-error for dynamic routes #66908

Open pathliving opened 1 week ago

pathliving commented 1 week ago

What?

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.

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 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.tsx cannot 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.

Fixes #66905

ijjk commented 1 week ago

Allow CI Workflow Run

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer