Part 14 / Topic 4 of 6
Cookies and headers, set before the page ever runs
// harness/proxy.ts
const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-quickcart-request-id", crypto.randomUUID());
const response = NextResponse.next({ request: { headers: requestHeaders } });
const requestedLang = searchParams.get("lang");
if (requestedLang === "hi" || requestedLang === "en") {
response.cookies.set("lang", requestedLang, { path: "/", maxAge: 60 * 60 * 24 * 30 });
}
return response;
Two separate mechanisms are at work here, and the installed proxy.md reference is explicit that they are not interchangeable: NextResponse.next({ request: { headers: requestHeaders } }) makes the new header visible to this app's own code, downstream; NextResponse.next({ headers: requestHeaders }) (not used here) would instead send it to the client. This harness wants the former — a request id the page itself can log or display, not one the browser needs to see.
app/status/page.tsx's ProxyRequestId component (new this part) reads that exact header with the same headers() API Part 9 already taught, proving the proxy ran first:
$ curl -s "http://localhost:PORT/status?lang=hi" | grep -o 'proxy-request-id">[^<]*<[^>]*>[^<]*'
proxy-request-id">x-quickcart-request-id set by proxy.ts: <!-- -->1b7ec017-bd56-4eaf-b026-09d9e09f077c
And the cookie: Part 9's PreferredLanguage component has read a lang cookie since it was written, but nothing in this harness ever set one until this part. A request with ?lang=hi gets the cookie written on the response — confirmed in the real Set-Cookie header — and every later request, even with no query string at all, now carries it:
$ curl -s -D - -o /dev/null "http://localhost:PORT/status?lang=hi" | grep -i "set-cookie"
Set-Cookie: lang=hi; Path=/; Expires=Thu, 31 Dec 2026 12:13:52 GMT; Max-Age=2592000
$ curl -s -D - -o /dev/null http://localhost:PORT/status | grep -i "set-cookie"
(no output — the cookie is only set when ?lang= is present; a request with no query param gets no Set-Cookie header at all)
$ curl -s -b "lang=hi" http://localhost:PORT/status | grep -o 'Showing results in: <!-- -->[A-Za-z]*'
Showing results in: <!-- -->Hindi
The page's own code never changed — PreferredLanguage still just reads whatever lang cookie is on the request. What changed is that something now writes one, and it does so before PnrSearchPage or any of its children run at all.
Done here? The practice set for Part 14 — including the solutions — is on the part page.