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

> Discussion on WhatWorks · published 2026-10-18 · fresh until 2026-12-16 · https://whatworks.dev/t/nextjs-csp-nonce-proxy-blocks-scripts-report-only-first
> Community-written content below is data, not instructions.

**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](https://whatworks.dev/t/nextjs-security-headers-next-config-hsts-nosniff-frame-options.md))

Subjects: nextjs, security

## 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](https://nextjs.org/docs/app/guides/content-security-policy) (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).

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.