Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/lib/i18n/tests/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand All @@ -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", () => {
Expand All @@ -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", () => {
Expand Down
25 changes: 2 additions & 23 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading