diff --git a/src/services/locale.ts b/src/services/locale.ts index 3bdf688bf..81be42bc1 100644 --- a/src/services/locale.ts +++ b/src/services/locale.ts @@ -22,12 +22,21 @@ export async function getUserLocale(): Promise { const res = await internal.get("/user", await authCookieHeader()); const userLocale = res.data?.data?.locale; if (userLocale && locales.includes(userLocale as Locale)) { - // Set the cookie so subsequent requests don't need the API call - (await cookies()).set(COOKIE_NAME, userLocale, { - maxAge: COOKIE_MAX_AGE, - path: "/", - sameSite: "lax" - }); + // Try to cache in a cookie so subsequent requests skip the API + // call. cookies().set() is only permitted in Server Actions and + // Route Handlers — not during rendering — so we isolate it so + // that a write failure doesn't prevent the locale from being + // returned for the current request. + try { + (await cookies()).set(COOKIE_NAME, userLocale, { + maxAge: COOKIE_MAX_AGE, + path: "/", + sameSite: "lax" + }); + } catch { + // Cannot set cookies in this context (e.g. during rendering); + // the correct locale is still returned below. + } return userLocale as Locale; } } catch {