QuiiBz / next-international

Type-safe internationalization (i18n) for Next.js
https://next-international.vercel.app
MIT License
1.28k stars 59 forks source link

app-static-to-dynamic-error with generateMetadata using getI18n on page not found #298

Open AvaN0x opened 10 months ago

AvaN0x commented 10 months ago

Describe the bug A clear and concise description of what the bug is.

Hello, I get a app-static-to-dynamic-error error when I go in a route that is not matched by the middleware.

To Reproduce Steps to reproduce the behavior:

  1. Clone this repro repo : https://github.com/AvaN0x/repro-next-international-generate-metadata
  2. npm i
    npm run build
    npm run start
  3. Click on the link in the page
  4. See the following error:
    ⨯ Page changed from static to dynamic at runtime /a.a, reason: headers
    see more here https://nextjs.org/docs/messages/app-static-to-dynamic-error

    image image

I also see that, when the path do not match the middleware, it'll still use the first part as the locale, this can be seein with the log locale not found a.a in the console.

Expected behavior I'd expect the not-found page to show, instead I get a 500 error. The error disappear when you remove the use of getI18n in the generateMetadata function in [locale]/layout.ts. The error does not occur in npm run dev.

About (please complete the following information):

Infos about the repro

I can provide more infos if needed

AvaN0x commented 10 months ago

I found a fix to the issue (the repro has been updated accordingly) with a call to setStaticParamsLocale in the generateMetadata function. This is related to this part of the code that'll check the headers if no staticParams were found: https://github.com/QuiiBz/next-international/blob/e4acd2fdf3c6ec181e218df82477f05df90c7311/packages/next-international/src/app/server/get-locale-cache.tsx#L19-L31


But I still get locale not found a.a logs in the layout by clicking the link in the repro. It would be nice if the params.locale value was always a valid locale.

Because of that, I have to add a check before passing the value to setStaticParamsLocale or redirect myself to the not found page.

https://github.com/AvaN0x/repro-next-international-generate-metadata/blob/80920471993de4b470e076d99254025e3abdff14/src/app/%5Blocale%5D/layout.tsx#L16-L18

https://github.com/AvaN0x/repro-next-international-generate-metadata/blob/80920471993de4b470e076d99254025e3abdff14/src/app/%5Blocale%5D/layout.tsx#L42-L46

locales and fallbackLocale are coming from my locales/shared.ts file.

QuiiBz commented 9 months ago

It would be nice if the params.locale value was always a valid locale.

Agree, that's why we use generateStaticParams to make sure the dynamic params are valid. This again seems like a bug with Next.js, I'll have to dig more.