Skip to content

feat: add first-class CSP nonce support - #5693

Merged
quantizor merged 1 commit into
mainfrom
feat/nonce-support
Mar 19, 2026
Merged

quantizor merged 1 commit into
mainfrom
feat/nonce-support

Conversation

@quantizor

Copy link
Copy Markdown
Contributor

Summary

Add multiple ways to configure a CSP nonce for injected <style> tags:

Method Use case
<StyleSheetManager nonce="..."> Next.js App/Pages Router, Remix — pass nonce from request headers
new ServerStyleSheet({ nonce: '...' }) SSR — explicit nonce in server rendering
<meta property="csp-nonce" nonce="..."> Vite — auto-detected at runtime
<meta name="sc-nonce" content="..."> Generic — auto-detected at runtime
__webpack_nonce__ Legacy webpack — preserved for backward compat

Framework integration examples

Next.js App Router:

// app/layout.tsx
const nonce = (await headers()).get('x-nonce');
<StyledComponentsRegistry nonce={nonce}>{children}</StyledComponentsRegistry>

Vite: Zero-config — set html.cspNonce in vite.config.js and the <meta property="csp-nonce"> tag is auto-detected.

Remix: Pass nonce from entry.server.tsx to <StyleSheetManager nonce={nonce}>.

Changes

  • StyleSheetManager — new nonce prop
  • ServerStyleSheet — new { nonce } constructor option
  • getNonce() — auto-detects from <meta property="csp-nonce"> (Vite) and <meta name="sc-nonce">; reads .nonce DOM property (not getAttribute) per browser security model
  • SheetOptions / makeStyleTag / Tag constructors — nonce threaded through

Based on #4356 by @ericsen-tsai. Seed commit attributed to the original author.

Test plan

  • All 504 web tests pass
  • All 37 native tests pass
  • Build passes (12.2KB gzip)
  • Manual test with Vite html.cspNonce
  • Manual test with Next.js middleware nonce

Closes #4258

@changeset-bot

changeset-bot Bot commented Mar 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 86b74fd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
styled-components Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@quantizor
quantizor force-pushed the feat/nonce-support branch from 40cf8bf to b5c7867 Compare March 19, 2026 19:00
quantizor added a commit to styled-components/styled-components-website that referenced this pull request Mar 19, 2026
- Add Content Security Policy section to security page with all nonce
  configuration methods (StyleSheetManager prop, ServerStyleSheet option,
  meta tag auto-detection, __webpack_nonce__)
- Add Next.js App Router CSP nonce guide with middleware + registry example
- Add Remix/React Router v7 CSP nonce guide with entry.server.tsx example
- Add Vite CSP nonce guide using html.cspNonce auto-detection
- Add nonce prop to StyleSheetManager API reference

Companion to styled-components/styled-components#5693
quantizor added a commit to styled-components/styled-components-website that referenced this pull request Mar 19, 2026
- Add Content Security Policy section to security page with all nonce
  configuration methods (StyleSheetManager prop, ServerStyleSheet option,
  meta tag auto-detection, __webpack_nonce__)
- Add Next.js App Router CSP nonce guide with middleware + registry example
- Add Remix/React Router v7 CSP nonce guide with entry.server.tsx example
- Add Vite CSP nonce guide using html.cspNonce auto-detection
- Add nonce prop to StyleSheetManager API reference

Companion to styled-components/styled-components#5693
@quantizor
quantizor force-pushed the feat/nonce-support branch 3 times, most recently from a37e67f to dd6af1e Compare March 19, 2026 19:19
Add multiple ways to configure a CSP nonce for injected <style> tags:

1. StyleSheetManager nonce prop (recommended for Next.js, Remix)
2. ServerStyleSheet constructor nonce option (SSR)
3. <meta property="csp-nonce"> auto-detection (Vite convention)
4. <meta name="sc-nonce"> auto-detection (SC convention)
5. __webpack_nonce__ global (legacy, preserved for backward compat)

The nonce flows through SheetOptions to makeStyleTag, ensuring all
dynamically created style elements receive the nonce attribute.

Based on PR #4356 by @ericsen-tsai.

Closes #4258
@quantizor
quantizor force-pushed the feat/nonce-support branch from dd6af1e to 86b74fd Compare March 19, 2026 19:23
@quantizor
quantizor marked this pull request as ready for review March 19, 2026 19:26
@quantizor
quantizor merged commit 79cc7b4 into main Mar 19, 2026
5 checks passed
@quantizor
quantizor deleted the feat/nonce-support branch March 19, 2026 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Support for Easily Configuring a Nonce.

2 participants