fix(seo): set <html lang> from URL locale, not from cookie

Cookies are absent on first visit and never sent by crawlers, so the
old cookie-based locale read in app/layout.tsx caused every search-
indexed /en, /fr and /it page to advertise lang="de". Middleware now
extracts the locale from the URL pathname and injects it as the
x-vita-locale request header (post-processing the next-intl response
so it survives both NextResponse.next and rewrite paths). Root layout
reads that header and validates it against routing.locales.

Verified end-to-end via curl: /de/, /en/, /fr/, /it/ (and locale-
prefixed auth pages) now each return the correct <html lang>.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.7 committed 2026-04-25 16:37:13 +02:00
1 parent 1519877e59
commit 5d77d3a9e5
3 files changed
+46 -6

No files matched your search

+10 -3
View File
@@ -2,7 +2,9 @@ import type { Metadata } from "next";
import { Cormorant_Garamond, DM_Sans } from "next/font/google";
import { COMPANY, SITE_URL } from "@/lib/config/company";
import { SessionProvider } from "@/components/portal/SessionProvider";
import { cookies } from "next/headers";
import { headers } from "next/headers";
import { LOCALE_HEADER } from "@/lib/config/routes";
import { routing } from "@/i18n/routing";
import "./globals.css";
const cormorant = Cormorant_Garamond({
@@ -62,8 +64,13 @@ export default async function RootLayout({
}: {
children: React.ReactNode;
}) {
const cookieStore = await cookies();
const locale = cookieStore.get("NEXT_LOCALE")?.value ?? "de";
// Locale is injected by proxy.ts middleware from the URL pathname so crawlers
// see the correct <html lang> on every request, regardless of cookies.
const headersList = await headers();
const headerLocale = headersList.get(LOCALE_HEADER);
const locale = (routing.locales as readonly string[]).includes(headerLocale ?? "")
? headerLocale!
: routing.defaultLocale;
const jsonLd = {
"@context": "https://schema.org",
+7
View File
@@ -28,3 +28,10 @@ export const ADMIN_ROUTES = {
documents: "/admin/documents",
reports: "/admin/reports",
} as const satisfies Record<string, string>;
/**
* Request header set by proxy.ts middleware containing the URL-derived locale.
* Read by app/layout.tsx so <html lang> reflects the actual URL — not a cookie
* (which crawlers don't carry, breaking SEO for non-default locales).
*/
export const LOCALE_HEADER = "x-vita-locale";
+29 -3
View File
@@ -3,7 +3,7 @@ import createIntlMiddleware from "next-intl/middleware";
import { routing } from "./i18n/routing";
import { NextResponse } from "next/server";
import { USER_ROLE } from "@/lib/config/auth";
import { PORTAL_ROUTES, ADMIN_ROUTES, AUTH_ROUTES } from "@/lib/config/routes";
import { PORTAL_ROUTES, ADMIN_ROUTES, AUTH_ROUTES, LOCALE_HEADER } from "@/lib/config/routes";
const intlMiddleware = createIntlMiddleware(routing);
@@ -20,11 +20,24 @@ const PORTAL_PREFIXES = [
// Derived from AUTH_ROUTES so adding a new auth page only requires updating lib/config/routes.ts
const LOCALE_AUTH_SUFFIXES = Object.values(AUTH_ROUTES);
/** Extract a routing locale from the URL pathname; falls back to the default locale. */
function detectLocale(pathname: string): string {
const seg = pathname.split("/")[1];
return (routing.locales as readonly string[]).includes(seg) ? seg : routing.defaultLocale;
}
export default auth((req) => {
const { pathname } = req.nextUrl;
const session = req.auth;
const dest = session?.user.role === USER_ROLE.admin ? ADMIN_ROUTES.patients : PORTAL_ROUTES.dashboard;
// Inject URL-derived locale as a request header so the root layout can set
// <html lang> from the actual URL (not from a cookie that crawlers don't carry).
const locale = detectLocale(pathname);
req.headers.set(LOCALE_HEADER, locale);
const passThrough = () =>
NextResponse.next({ request: { headers: req.headers } });
// ── Portal / admin ─────────────────────────────────────────────────────
if (PORTAL_PREFIXES.some((p) => pathname === p || pathname.startsWith(p + "/"))) {
if (!session) {
@@ -35,7 +48,7 @@ export default auth((req) => {
if (pathname.startsWith(ADMIN_ROUTES.root) && session.user.role !== USER_ROLE.admin) {
return NextResponse.redirect(new URL(PORTAL_ROUTES.dashboard, req.url));
}
return NextResponse.next();
return passThrough();
}
// ── Locale-prefixed portal paths: redirect to canonical ───────────────
@@ -64,7 +77,20 @@ export default auth((req) => {
return NextResponse.redirect(new URL(dest, req.url));
}
return intlMiddleware(req);
// Delegate to next-intl, then ensure our LOCALE_HEADER reaches downstream
// rendering by re-emitting the response with the modified request headers.
// (intl's NextResponse.next/rewrite doesn't include our request.headers init.)
const intlResp = intlMiddleware(req);
const rewriteUrl = intlResp.headers.get("x-middleware-rewrite");
const redirectUrl = intlResp.headers.get("location");
if (redirectUrl) return intlResp; // redirects don't render the layout
const wrapped = rewriteUrl
? NextResponse.rewrite(rewriteUrl, { request: { headers: req.headers } })
: NextResponse.next({ request: { headers: req.headers } });
intlResp.headers.forEach((v, k) => {
if (k !== "x-middleware-rewrite" && k !== "location") wrapped.headers.set(k, v);
});
return wrapped;
});
export const config = {