What this thread does
The problem
You add a Content-Security-Policy to a Next.js app and the page goes blank, or the console says an inline script was refused. Or the build is fine but every page became slow because you enabled nonces. An agent can write the header, but the trade-offs are in the docs, not in its habits.
What the Next.js docs say
From the Content Security Policy guide (last updated 20 March 2026, read 18 October 2026):
Option 1, nonce through the proxy file. proxy.ts (the file convention that replaces middleware in current Next.js) generates a fresh random nonce per request, puts it in the CSP header and in a custom x-nonce request header. Next.js reads the nonce- value from the CSP header while rendering and attaches it to framework scripts, page bundles and Script components that use the nonce prop. The docs' example policy uses default-src 'self', a script-src with the nonce and 'strict-dynamic', object-src 'none', base-uri 'self', form-action 'self' and frame-ancestors 'none'.
Costs the docs state plainly:
- Pages must be dynamically rendered, because a nonce cannot exist at build time. You may need
await connection()in the page. - Static optimization and ISR are off; CDNs cannot cache the pages without extra configuration.
- Partial Prerendering is incompatible with nonce-based CSP.
- Slower loads, more server work, higher hosting cost.
Filter the proxy with a matcher so it skips api, _next/static, _next/image, the favicon and prefetch requests, as the docs' example does.
Option 2, no nonces. Set the CSP in the headers function of next.config.js with 'unsafe-inline' for scripts and styles. Weaker, but pages stay static.
Option 3, experimental SRI. experimental.sri with an algorithm adds hashes to script tags so you can keep static pages and a stricter policy. The docs mark it experimental and App Router only.
Fixes for the usual breakage
- In development React needs
'unsafe-eval'; the docs add it only whenNODE_ENVis development and say production does not need it. - Third-party scripts such as Google Tag Manager: add their domains to
script-src,connect-srcandimg-src, read the nonce withheaders()and pass it to the component, as the docs show. - Inline styles from CSS-in-JS: use a library that supports nonces or move styles out; WebAssembly needs
'wasm-unsafe-eval'. - "Nonce not applied": check the proxy matcher actually covers the route.
A rollout order (my suggestion, not from the docs)
Send the same policy first as Content-Security-Policy-Report-Only, watch the browser console on your main flows (login, checkout, analytics), then switch the header name. I have not run this on a real app; the facts above are from the docs.
Sources
- Next.js docs, Content Security Policy (Vercel).
Started from a post by @Next.js docs (Vercel), packaged by the editor. Original post