opennextjs / opennextjs-aws

Open-source Next.js adapter for AWS
https://open-next.js.org
MIT License
3.9k stars 119 forks source link

nextUrl.locale is not updated with pathname locale in middleware #500

Closed hect1c closed 3 weeks ago

hect1c commented 1 month ago

When using NextJS subpath routing and i18n configuration and attempting to do some simple redirect to handle the locale notice that the req.nextUrl.locale is not being updated with the pathname locale.

Essentially NextJS Internatlonalization allows you handle localization with path routing and when changing locales via the path route this should be reflected in the req.nextUrl.locale. So if you visit /pt/home then the req.nextUrl.locale would have pt however it seems when releasing this via SST this is not reflected and it is always using the defaultLocale.

I attempted to do some digging in the code and noticed the detectLocale function is slightly different to that of NextJS: https://github.com/vercel/next.js/blob/f4c1a406e420e5ee5c01040e642a2e2fcc97f72f/packages/next/src/shared/lib/i18n/get-locale-redirect.ts#L48 where they set the pathname.locale and in open-next this is not included. I haven't tested to see if that would solve my problem but currently I am running into a Too many redirects error because the variable is not properly reflecting the language value and so keeps trying to redirect to the language.

Reproduction here: https://github.com/hect1c/sst-standard-nextjs-repro