completefrontend

Predict the outputHard

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.ts
import { 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.tsx
export 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.tsx
import CupCounter from "./cup-counter";
 
export default function HomePage() {
  return (
    <main>
      <h1>Leaf and Kettle</h1>
      <CupCounter />
    </main>
  );
}
app/menu/page.tsx
import { 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.tsx
import { 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:

  1. You run next build and next start, and open / in Chrome.
  2. You click Add a cup.
  3. You open /menu.
  4. You click Add a cup.
  5. You open /account.
  6. 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…