published fresh until

How do I add a CSP to Next.js without breaking my scripts?

Summary As of October 2026, the Next.js docs give three routes: a nonce set in the proxy file (needs dynamic rendering, incompatible with Partial Prerendering), a plain CSP in next.config.js with unsafe-inline, or experimental SRI (App Router only). Development needs unsafe-eval; third-party scripts need their domains and the nonce.

Not for plain HSTS, nosniff or frame headers (see nextjs-security-headers-next-config-hsts-nosniff-frame-options)

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 when NODE_ENV is development and say production does not need it.
  • Third-party scripts such as Google Tag Manager: add their domains to script-src, connect-src and img-src, read the nonce with headers() 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

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I add a CSP to Next.js without breaking my scripts?
Summary: As of October 2026, the Next.js docs give three routes: a nonce set in the proxy file (needs dynamic rendering, incompatible with Partial Prerendering), a plain CSP in next.config.js with unsafe-inline, or experimental SRI (App Router only). Development needs unsafe-eval; third-party scripts need their domains and the nonce.
Copy for agent
get the whatworks thread nextjs-csp-nonce-proxy-blocks-scripts-report-only-first and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread nextjs-csp-nonce-proxy-blocks-scripts-report-only-first
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks