AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Nonce-Based CSP With Inline <Head> Scripts Is Incompatible With CacheComponents

When cacheComponents is enabled, reading headers() to get CSP nonce in root layout for blocking inline head scripts either fails build (outside Suspense) or defers script execution (inside Suspense), making nonce-based CSP unusable for critical inline scripts.

highConfidence 85%Next.jsReact Server Componentsnext buildAffected V16.0.0Affected V16.1.5

Origin Analysis

cacheComponents requires the root layout to be static/prerenderable for routes like /_not-found. Accessing headers() counts as uncached dynamic data. Outside Suspense it forces dynamic rendering and fails build; inside Suspense the content is streamed after initial HTML shell, so scripts injected via React hydration are no longer parser-blocking. Next.js lacks a built-in way to inject per-request nonce into custom inline scripts while keeping shell static.
1. Create Next.js 16 app with cacheComponents: true in next.config.js. 2. Set up middleware/proxy to generate x-nonce header and add CSP with nonce. 3. In app/layout.tsx, read headers() to get nonce and render a blocking inline script in <head> with nonce attribute and dangerouslySetInnerHTML. 4. Run npm run build. Build fails with 'Uncached data was accessed outside of <Suspense>' for /_not-found. 5. Wrap nonce script in <Suspense> instead. Build succeeds but script is streamed after initial HTML, no longer blocking.

Fixing Code Block

Edge Case Audit

This workaround only works for static inline scripts whose content is known at build time. Dynamic inline scripts (containing server-injected values) cannot be covered by hash and remain unsupported. If the script content is modified without updating the middleware hash, CSP will block execution. The hash-source is per-script; multiple inline scripts each need their own hash. Rollback: remove the middleware hash logic and revert to calling headers() inside a <Suspense> boundary (accepting late execution) or disable cacheComponents.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state