Part 15 / Topic 5 of 5
Server Component vs. middleware: measured, not just described
Section 4's authoritative check (requireUser() inside the page) has one real cost under this install's Cache Components model: because cookies() can only be read behind a <Suspense> boundary, a redirect it triggers always streams back as HTTP/1.1 200 OK, with the redirect embedded in the RSC payload for the client to act on after hydration — not a real top-level redirect status, reproduced on every single request, not just a first-hit cache-miss:
$ curl -s -D - -o /dev/null http://localhost:PORT/account/dashboard # DAL-only, no proxy check, 3 repeated requests
HTTP/1.1 200 OK (request 1)
HTTP/1.1 200 OK (request 2)
HTTP/1.1 200 OK (request 3)
proxy.ts (Part 14's file) now carries a second check, added for this part, using iron-session's own nextProxyCookies adapter — built, per its own source comments, specifically for "Next.js proxy.ts (called middleware.ts before Next 16)":
// harness/proxy.ts (excerpt)
if (pathname.startsWith("/account/dashboard")) {
const response = NextResponse.next();
const session = await getIronSession<SessionData>(nextProxyCookies(request, response), sessionOptions);
if (!session.userId) {
return NextResponse.redirect(new URL("/account/login", request.url));
}
}
With this in place, the exact same unauthenticated request now gets a real, immediate redirect, before any React rendering happens at all:
$ curl -s -D - -o /dev/null http://localhost:PORT/account/dashboard
HTTP/1.1 307 Temporary Redirect
location: /account/login
This is the installed docs' own "optimistic check" framing, confirmed by measurement rather than assumed: the proxy's check is cheap (one cookie decrypt, no database read, no React render tree at all) and produces a genuinely fast, genuinely real redirect — useful for the common case and for not wasting a render on an obviously-logged-out visitor. But it is still explicitly not the authoritative check — the installed docs state plainly that Proxy "should not be used as a full session management or authorization solution," and section 4 already proved why: if this exact pathname.startsWith("/account/dashboard") check were ever forgotten in a matcher update (Part 14 section 6's exact warning), requireUser() inside the page is the only thing standing between an unauthenticated visitor and real content. This course keeps both: the proxy check for a fast, optimistic redirect on the common path; the DAL check inside the page for the actual guarantee.
Done here? The practice set for Part 15 — including the solutions — is on the part page.