diff --git a/src/lib/i18n/tests/middleware.test.ts b/src/lib/i18n/tests/middleware.test.ts index baf8bd7..d50508c 100644 --- a/src/lib/i18n/tests/middleware.test.ts +++ b/src/lib/i18n/tests/middleware.test.ts @@ -4,12 +4,12 @@ import { middleware } from "@/middleware"; import { LOCALE_COOKIE } from "@/lib/i18n/core"; describe("middleware transparent locale rewrite", () => { - it("rewrites / to /[defaultLocale] and sets cookie when no cookie exists", () => { + it("rewrites / to /[defaultLocale] without setting cookie so responses stay cacheable", () => { const req = new NextRequest("http://localhost:3000/"); const res = middleware(req); expect(res.headers.get("x-middleware-rewrite")).toBe("http://localhost:3000/en"); - expect(res.cookies.get(LOCALE_COOKIE)?.value).toBe("en"); + expect(res.cookies.get(LOCALE_COOKIE)).toBeUndefined(); }); it("redirects /leaderboard to /ar/leaderboard when app-locale cookie is ar", () => { @@ -35,7 +35,7 @@ describe("middleware transparent locale rewrite", () => { expect(res.status).toBe(307); expect(res.headers.get("location")).toBe("http://localhost:3000/ar/scoring-methodology"); - expect(res.cookies.get(LOCALE_COOKIE)?.value).toBe("ar"); + expect(res.cookies.get(LOCALE_COOKIE)).toBeUndefined(); }); it("rewrites / based on English Accept-Language when no cookie exists", () => { @@ -49,7 +49,7 @@ describe("middleware transparent locale rewrite", () => { expect(res.headers.get("x-middleware-rewrite")).toBe( "http://localhost:3000/en/scoring-methodology", ); - expect(res.cookies.get(LOCALE_COOKIE)?.value).toBe("en"); + expect(res.cookies.get(LOCALE_COOKIE)).toBeUndefined(); }); it("passes through without rewrite if path already starts with supported locale", () => { diff --git a/src/middleware.ts b/src/middleware.ts index 24ba643..bed6bf1 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -46,40 +46,19 @@ export function middleware(request: NextRequest) { const redirectUrl = request.nextUrl.clone(); redirectUrl.pathname = `/${locale}${pathname === "/" ? "" : pathname}`; - const response = NextResponse.redirect(redirectUrl, { + return NextResponse.redirect(redirectUrl, { status: 307, headers: { "Cache-Control": "private, no-cache, no-store, must-revalidate", }, }); - - if (!hasValidCookie) { - response.cookies.set(LOCALE_COOKIE, locale, { - path: "/", - maxAge: 60 * 60 * 24 * 365, - sameSite: "lax", - }); - } - - return response; } // Default locale (English): transparent rewrite to /en/... so unprefixed URLs stay canonical const rewriteUrl = request.nextUrl.clone(); rewriteUrl.pathname = `/${DEFAULT_LOCALE}${pathname === "/" ? "" : pathname}`; - const response = NextResponse.rewrite(rewriteUrl); - - // If cookie wasn't set or was invalid, set it now for persistence - if (!hasValidCookie) { - response.cookies.set(LOCALE_COOKIE, DEFAULT_LOCALE, { - path: "/", - maxAge: 60 * 60 * 24 * 365, - sameSite: "lax", - }); - } - - return response; + return NextResponse.rewrite(rewriteUrl); } // Run only on routes that produce HTML or read the cookie. Skip Next.js