Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Fix language preference reset by writing locale cookie client-side (#2765)
Luke Vella9a7cc82abce6
diff --git a/apps/web/src/app/[locale]/(space)/settings/preferences/components/language-preference.tsx b/apps/web/src/app/[locale]/(space)/settings/preferences/components/language-preference.tsxindex cd29b4a37..d02494886 100644--- a/apps/web/src/app/[locale]/(space)/settings/preferences/components/language-preference.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/preferences/components/language-preference.tsx@@ -17,7 +17,7 @@ import * as z from "zod";import { LanguageSelect } from "@/components/language-selector";import { updateLocalizationAction } from "@/features/user/actions";import { Trans } from "@/i18n/client";-import { useLocale } from "@/lib/locale/client";+import { setLocaleCookie, useLocale } from "@/lib/locale/client";import { useSafeAction } from "@/lib/safe-action/client";const formSchema = z.object({@@ -48,12 +48,33 @@ export const LanguagePreference = ({<Form {...form}><formonSubmit={form.handleSubmit(async (data) => {- const result = await updateLocalization.executeAsync({- locale: data.language,- });- if (!result?.serverError && !result?.validationErrors) {- form.reset({ language: data.language });+ // Set the cookie before executing: useSafeAction refreshes the router+ // on success, and that refresh must read the new locale. Writing the+ // cookie server-side would collide with updateUser's session cookie+ // and drop it. Nothing reconciles the cookie against the saved value+ // on load, so roll it back to the locale this page is rendered in if+ // the save doesn't land.+ const previousLocale = locale;+ setLocaleCookie(data.language);++ let result: Awaited<+ ReturnType<typeof updateLocalization.executeAsync>+ >;+ try {+ result = await updateLocalization.executeAsync({+ locale: data.language,+ });+ } catch (error) {+ setLocaleCookie(previousLocale);+ throw error;+ }++ if (result?.serverError || result?.validationErrors) {+ setLocaleCookie(previousLocale);+ return;}++ form.reset({ language: data.language });})}><FormFielddiff --git a/apps/web/src/features/user/actions.ts b/apps/web/src/features/user/actions.tsindex 8a79b72ae..4a16b30b8 100644--- a/apps/web/src/features/user/actions.ts+++ b/apps/web/src/features/user/actions.ts@@ -8,7 +8,6 @@ import { banUser, unbanUser, updateUserRole } from "@/features/user/mutations";import authLib from "@/lib/auth";import { timeFormatSchema, weekStartSchema } from "@/lib/datetime/schema";import { AppError } from "@/lib/errors/app-error";-import { setLocaleCookie } from "@/lib/locale/cookie";import {adminActionClient,authActionClient,@@ -55,6 +54,9 @@ export const updateLocalizationAction = authActionClient}),).action(async ({ parsedInput }) => {+ // The locale cookie is written client-side (see language-preference.tsx).+ // Writing it here via next/headers would collide with updateUser's session+ // cookie and drop it.await authLib.api.updateUser({body: {locale: parsedInput.locale,@@ -64,10 +66,6 @@ export const updateLocalizationAction = authActionClient},headers: await headers(),});-- if (parsedInput.locale) {- await setLocaleCookie(parsedInput.locale);- }});export const getAvatarUploadUrlAction = authActionClientdiff --git a/apps/web/src/lib/locale/client.tsx b/apps/web/src/lib/locale/client.tsxindex 32326b63f..0a6595c15 100644--- a/apps/web/src/lib/locale/client.tsx+++ b/apps/web/src/lib/locale/client.tsx@@ -1,5 +1,10 @@"use client";+import Cookies from "js-cookie";import { useParams } from "next/navigation";+import {+ LOCALE_COOKIE_NAME,+ LOCALE_COOKIE_OPTIONS,+} from "@/lib/locale/constants";export function useLocale() {const { locale } = useParams();@@ -8,3 +13,10 @@ export function useLocale() {locale: locale as string,};}++// Set the locale cookie from the client so it lands synchronously, before the+// router refresh reads it. Writing it server-side in the same request as+// better-auth's updateUser would collide with its session cookie and drop it.+export function setLocaleCookie(locale: string) {+ Cookies.set(LOCALE_COOKIE_NAME, locale, LOCALE_COOKIE_OPTIONS);+}diff --git a/apps/web/src/lib/locale/cookie.ts b/apps/web/src/lib/locale/cookie.tsdeleted file mode 100644index fc112bbe4..000000000--- a/apps/web/src/lib/locale/cookie.ts+++ /dev/null@@ -1,22 +0,0 @@-import "server-only";-import { supportedLngs } from "@rallly/languages";-import { cookies } from "next/headers";-import {- LOCALE_COOKIE_NAME,- LOCALE_COOKIE_OPTIONS,-} from "@/lib/locale/constants";--export async function getLocaleCookie() {- const cookieStore = await cookies();- const locale = cookieStore.get(LOCALE_COOKIE_NAME)?.value;- return locale && supportedLngs.includes(locale) ? locale : undefined;-}--// Server-action/RSC contexts only. Never call from better-auth hooks:-// mutating Next's cookie store during a route-handler request makes Next-// rebuild the handler's Set-Cookie headers through a name-keyed map,-// dropping duplicate-name cookies (see the locale writes in lib/auth.ts).-export async function setLocaleCookie(locale: string) {- const cookieStore = await cookies();- cookieStore.set(LOCALE_COOKIE_NAME, locale, LOCALE_COOKIE_OPTIONS);-}