Skip to content

[16.3.x] Add CSP nonce to script tags of loading and template files - #98403

Merged
mischnic merged 1 commit into
next-16-3from
mischnic/backport-csp
Sep 10, 2026
Merged

mischnic merged 1 commit into
next-16-3from
mischnic/backport-csp

Conversation

@mischnic

@mischnic mischnic commented Sep 9, 2026

Copy link
Copy Markdown
Member

Backport #98398

Recreation of #98152 so that
deployment test credentials are available in CI
Closes #98152
Closes #92803
Original description:

> `createComponentStylesAndScripts` renders the script tags for a
segment's loading, error, and templatefound files, but unlike
getLayerAssets it did not pass `ctx.nonce`. When such a file has a chunk
that no layout or client reference has already loaded, the tag is served
without a nonce and a nonce-based Content-Security-Policy blocks it.

### What?

Patches a bug we encountered while using
[nonces](https://nextjs.org/docs/app/guides/content-security-policy#nonces)
for a CSP with `strict-dynamic`, as in the docs example. In Turbopack,
when a `loading.tsx` (or `error` or `template`) file has a chunk that
isn’t already loaded by a layout or client component on the page, Next
renders a `<script>` tag for that chunk with no `nonce`. (the same chunk
script would’ve gotten a `nonce` if it were emitted for a layout or
page).

Supersedes #92803; this is the same fix but with a new test included. 

### Why?

While building out our `loading.tsx` pages, we encountered browsers
blocking a script and reporting CSP violations, because chunk scripts
did not properly carry a `nonce`.

### How?

One-line change to bring `createComponentStylesAndScripts` to parity
with `getLayerAssets`.

---------

Co-authored-by: Luke Taylor <luket@anthropic.com>
@mischnic
mischnic requested a review from a team September 9, 2026 08:15
@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 43b0403

@mischnic
mischnic merged commit 2b1f28d into next-16-3 Sep 10, 2026
128 checks passed
@mischnic
mischnic deleted the mischnic/backport-csp branch September 10, 2026 11:27
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Sep 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants