Locale cookie may not be updated (Vercel) #936
-
When using static rendering in Vercel, further requests to the same page will get a 304 response that lacks the This problem can be reproduced in the App Router Example available in the docs:
I verified that the middleware is executed any way, but I believe Vercel strips the As a workaround for this, I created a client component with the sole responsibility of updating the cookie on first load and placed it on the layout: // components/syncLocale.tsx
export const SyncLocale = ({ locale, path }: SyncLocaleProps) => {
useEffect(() => {
document.cookie = `${COOKIE_LOCALE_NAME}=${locale}; path=${path}; max-age=${COOKIE_MAX_AGE}; sameSite=${COOKIE_SAME_SITE}`
}, [locale, path])
return null
}
//app/[locale]/layout.tsx
export default function RootLayout({
children,
params: { locale },
}: Readonly<{
children: React.ReactNode
params: { locale: string }
}>) {
unstable_setRequestLocale(locale)
return (
<html lang={locale}>
<SyncLocale locale={locale} path="/" />
<body>
// ...
</body>
</html>
)
} Any suggestions / experiences on this would be much appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @rodrigovcortezi, thanks for the great question! I've noticed a similar issue when the cache from the client-side router is used and have addressed this in #790. The result is that now every time a navigation API from But back to your issue: Vercel can return a 304 response for cached routes. However, a 304 status code (that lacks the This means that if the user starts on So while ideally I'd really like to avoid this problem, I'm unsure if practically users will really run into this. That being said, if there's a way Your fix with Hope this provides some further context! |
Beta Was this translation helpful? Give feedback.
Hey @rodrigovcortezi, thanks for the great question!
I've noticed a similar issue when the cache from the client-side router is used and have addressed this in #790. The result is that now every time a navigation API from
next-intl
is used to switch the locale, the cookie value is kept in sync, regardless of whether the middleware is hit or not.But back to your issue: Vercel can return a 304 response for cached routes. However, a 304 status code (that lacks the
set-cookie
response header) can only be issued if the user has visited this very route in the browser previously and the browser sends anIf-None-Match
request header along with the request.This means that if the user starts on
/de
…