QuiiBz / next-international

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

feat(next-international): add `defaultLocale` config option to `createI18nServer` #365

Open zfm-lucaschultz opened 4 months ago

zfm-lucaschultz commented 4 months ago

Hi, thank you for the wonderful package.

We have started using it in our project and encountered a small edge case. Instead of placing the entire app into an app/[locale] folder, we determine the locale by fetching the user in the middleware, checking the preferred language, and then using a modified version of your middleware to set the cookie and header values. While I believe this scenario is common, for now, we are using it incorrectly 😅

This situation has led to an interesting edge case. Our app has almost no static routes because we rely on headers extensively for fetching. Consequently, we do not invoke setStaticParamsLocale at the top of our pages. However, during app building, Next.js assumes that all routes without parameters are static, attempts to prerender them, and only treats the route as dynamic upon encountering a headers or cookies call. Unfortunately, in practice, it does not reach this stage as we encounter an error before that point:

Error: Could not find locale while pre-rendering page, make sure you called `setStaticParamsLocale` at the top of your pages

To address this, I propose introducing a defaultLocale on the server for prerendering if setStaticParamsLocale was not called. Alternatively, defaultStaticLocale may be a more suitable option.

vercel[bot] commented 4 months ago

@zfm-lucaschultz is attempting to deploy a commit to the Tom Labs Team on Vercel.

A member of the Team first needs to authorize it.

lucaschultz commented 4 months ago

Hi,

Could you share your middleware code?

Sure thing! Here is a minimal example that reproduces the problem I was describing. I added a description of what I think is happening to the readme. I hope this helps somewhat in understanding what I meant. 🙂

It also implements the middleware/route structure like we are using it with next-international. If I haven't missed anything glaringly obvious, it's pretty straightforward to adapt your library to our use case. In my opinion, the only thing missing is exporting addLocaleToResponse and documenting how to set it up.

QuiiBz commented 3 months ago

Oh I see the issue you're running into! The thing is that you have to move all pages inside a [locale] folder (dynamic segment). Sorry for the delay btw.