proxy.ts adds a Content Security Policy with a nonce. Which still works: a static page, one with a static shell, or a dynamic one?
The tea shop adds a strict Content Security Policy: proxy.ts makes a random nonce for every request, allows only scripts that carry it, and passes the policy on to the app as a request header, so Next.js can put the nonce on its own scripts. Three pages show the same Client Component, a cup counter with an Add a cup button. The home page reads nothing from the request, the menu streams a part that waits for the request inside <Suspense>, and the account page waits for the request before anything else. What does next build print, and what happens when you press Add a cup on each page?
proxy.tsimport { NextResponse, type NextRequest } from "next/server";
export function proxy(request: NextRequest) {
const nonce = Buffer.from(crypto.getRandomValues(new Uint8Array(16))).toString("base64");
const policy = [
"default-src 'self'",
`script-src 'self' 'nonce-${nonce}' 'strict-dynamic'`,
`style-src 'self' 'nonce-${nonce}'`,
"object-src 'none'",
"base-uri 'self'",
"frame-ancestors 'none'",
].join("; ");
// Next.js reads the nonce from the request's policy, so the app gets it too.
const requestHeaders = new Headers(request.headers);
requestHeaders.set("content-security-policy", policy);
const response = NextResponse.next({ request: { headers: requestHeaders } });
response.headers.set("content-security-policy", policy);
return response;
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};
app/layout.tsxexport const metadata = { title: "Leaf and Kettle" };
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
app/cup-counter.tsx"use client";
import { useState } from "react";
export default function CupCounter() {
const [cups, setCups] = useState(0);
return (
<p>
Cups: {cups} <button onClick={() => setCups(cups + 1)}>Add a cup</button>
</p>
);
}
app/page.tsximport CupCounter from "./cup-counter";
export default function HomePage() {
return (
<main>
<h1>Leaf and Kettle</h1>
<CupCounter />
</main>
);
}
app/menu/page.tsximport { Suspense } from "react";
import { connection } from "next/server";
import CupCounter from "../cup-counter";
async function FreshPot() {
await connection();
return <p>A fresh pot of mint tea is brewing.</p>;
}
export default function MenuPage() {
return (
<main>
<h1>Today's teas</h1>
<Suspense fallback={<p>Checking the pot…</p>}>
<FreshPot />
</Suspense>
<CupCounter />
</main>
);
}
app/account/page.tsximport { connection } from "next/server";
import CupCounter from "../cup-counter";
export const instant = false;
export default async function AccountPage() {
await connection();
return (
<main>
<h1>Your account</h1>
<CupCounter />
</main>
);
}
It's a project create-next-app made (Next.js 16.4.3, TypeScript, the App Router, and Cache Components on) with these files, built with next build and run with next start. What happens:
- You run
next buildandnext start, and open/in Chrome. - You click Add a cup.
- You open
/menu. - You click Add a cup.
- You open
/account. - You click Add a cup.
The answer
Work out what Next.js does at each step: what the build prints, what each request answers, and what the page shows. Then check.
Checking your sign-in…