Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
544f5212
Unverified
Commit
544f5212
authored
Sep 04, 2025
by
Trevor Burnham
Committed by
GitHub
Sep 04, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: Include CSP nonce in next/dynamic preload (#81999)
Co-authored-by:
Jiachi Liu
<
inbox@huozhi.im
>
parent
1bfe63a6
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
146 additions
and
1 deletion
+146
-1
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+3
-1
work-async-storage.external.ts
...next/src/server/app-render/work-async-storage.external.ts
+1
-0
work-store.ts
packages/next/src/server/async-storage/work-store.ts
+3
-0
preload-chunks.tsx
packages/next/src/shared/lib/lazy-dynamic/preload-chunks.tsx
+2
-0
dynamic-component.js
...e/app-dir/next-dynamic-csp-nonce/app/dynamic-component.js
+9
-0
layout.js
test/e2e/app-dir/next-dynamic-csp-nonce/app/layout.js
+29
-0
page.js
test/e2e/app-dir/next-dynamic-csp-nonce/app/page.js
+16
-0
middleware.js
test/e2e/app-dir/next-dynamic-csp-nonce/middleware.js
+16
-0
next-dynamic-csp-nonce.test.ts
...dir/next-dynamic-csp-nonce/next-dynamic-csp-nonce.test.ts
+61
-0
next.config.js
test/e2e/app-dir/next-dynamic-csp-nonce/next.config.js
+6
-0
No files found.
packages/next/src/server/app-render/app-render.tsx
View file @
544f5212
...
...
@@ -1982,7 +1982,8 @@ export const renderToHTMLOrFlight: AppPageRender = (
previewModeId
:
renderOpts
.
previewProps
?.
previewModeId
,
})
const
{
isPrefetchRequest
,
previouslyRevalidatedTags
}
=
parsedRequestHeaders
const
{
isPrefetchRequest
,
previouslyRevalidatedTags
,
nonce
}
=
parsedRequestHeaders
let
postponedState
:
PostponedState
|
null
=
null
...
...
@@ -2018,6 +2019,7 @@ export const renderToHTMLOrFlight: AppPageRender = (
isPrefetchRequest
,
buildId
:
sharedContext
.
buildId
,
previouslyRevalidatedTags
,
nonce
,
})
return
workAsyncStorage
.
run
(
...
...
packages/next/src/server/app-render/work-async-storage.external.ts
View file @
544f5212
...
...
@@ -97,6 +97,7 @@ export interface WorkStore {
Record
<
string
,
{
files
:
string
[]
}
>
>
readonly
assetPrefix
?:
string
readonly
nonce
?:
string
cacheComponentsEnabled
:
boolean
dev
:
boolean
...
...
packages/next/src/server/async-storage/work-store.ts
View file @
544f5212
...
...
@@ -20,6 +20,7 @@ export type WorkStoreContext = {
page
:
string
isPrefetchRequest
?:
boolean
nonce
?:
string
renderOpts
:
{
cacheLifeProfiles
?:
{
[
profile
:
string
]:
CacheLife
}
incrementalCache
?:
IncrementalCache
...
...
@@ -79,6 +80,7 @@ export function createWorkStore({
isPrefetchRequest
,
buildId
,
previouslyRevalidatedTags
,
nonce
,
}:
WorkStoreContext
):
WorkStore
{
/**
* Rules of Static & Dynamic HTML:
...
...
@@ -135,6 +137,7 @@ export function createWorkStore({
buildId
,
reactLoadableManifest
:
renderOpts
?.
reactLoadableManifest
||
{},
assetPrefix
:
renderOpts
?.
assetPrefix
||
''
,
nonce
,
afterContext
:
createAfterContext
(
renderOpts
),
cacheComponentsEnabled
:
renderOpts
.
experimental
.
cacheComponents
,
...
...
packages/next/src/shared/lib/lazy-dynamic/preload-chunks.tsx
View file @
544f5212
...
...
@@ -58,6 +58,7 @@ export function PreloadChunks({
href=
{
href
}
rel=
"stylesheet"
as=
"style"
nonce=
{
workStore
.
nonce
}
/>
)
}
else
{
...
...
@@ -65,6 +66,7 @@ export function PreloadChunks({
preload
(
href
,
{
as
:
'script'
,
fetchPriority
:
'low'
,
nonce
:
workStore
.
nonce
,
})
return
null
}
...
...
test/e2e/app-dir/next-dynamic-csp-nonce/app/dynamic-component.js
0 → 100644
View file @
544f5212
'use client'
export
default
function
DynamicComponent
()
{
return
(
<
div
id
=
"dynamic-component-loaded"
>
Dynamic
component
loaded
successfully
<
/div
>
)
}
test/e2e/app-dir/next-dynamic-csp-nonce/app/layout.js
0 → 100644
View file @
544f5212
import
{
headers
}
from
'next/headers'
import
{
Suspense
}
from
'react'
async
function
CSPMetatag
({
children
})
{
const
resolvedHeaders
=
await
headers
()
const
nonce
=
resolvedHeaders
.
get
(
'x-nonce'
)
||
'test-nonce'
return
(
<
meta
httpEquiv
=
"Content-Security-Policy"
content
=
{
`default-src 'self'; script-src 'self' 'nonce-
${
nonce
}
'; style-src 'self' 'unsafe-inline'`
}
/
>
)
}
export
default
async
function
RootLayout
({
children
})
{
return
(
<
html
>
<
Suspense
>
<
head
>
<
CSPMetatag
/>
<
/head
>
<
body
>
<
div
id
=
"csp-nonce-test"
>
{
children
}
<
/div
>
<
/body
>
<
/Suspense
>
<
/html
>
)
}
test/e2e/app-dir/next-dynamic-csp-nonce/app/page.js
0 → 100644
View file @
544f5212
'use client'
import
dynamic
from
'next/dynamic'
const
DynamicComponent
=
dynamic
(()
=>
import
(
'./dynamic-component'
),
{
loading
:
()
=>
<
div
id
=
"loading"
>
Loading
...
<
/div>
,
})
export
default
function
CSPNoncePage
()
{
return
(
<
div
>
<
h1
id
=
"page-title"
>
CSP
Nonce
Test
Page
<
/h1
>
<
DynamicComponent
/>
<
/div
>
)
}
test/e2e/app-dir/next-dynamic-csp-nonce/middleware.js
0 → 100644
View file @
544f5212
import
{
NextResponse
}
from
'next/server'
export
function
middleware
(
request
)
{
const
response
=
NextResponse
.
next
()
const
nonce
=
'test-nonce'
response
.
headers
.
set
(
'x-nonce'
,
nonce
)
response
.
headers
.
set
(
'Content-Security-Policy'
,
`default-src 'self'; script-src 'self' 'nonce-
${
nonce
}
'; style-src 'self' 'unsafe-inline'`
)
return
response
}
export
const
config
=
{
matcher
:
'/:path*'
,
}
test/e2e/app-dir/next-dynamic-csp-nonce/next-dynamic-csp-nonce.test.ts
0 → 100644
View file @
544f5212
import
{
nextTestSetup
}
from
'e2e-utils'
describe
(
'next/dynamic with CSP nonce'
,
()
=>
{
const
{
next
}
=
nextTestSetup
({
files
:
__dirname
,
})
it
(
'should include nonce attribute on preload links generated by next/dynamic'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
'/'
)
// Check that preload links have the nonce attribute
const
preloadLinks
=
$
(
'link[rel="preload"]'
)
const
dynamicPreloadLinks
=
preloadLinks
.
filter
((
_
,
element
)
=>
{
const
$element
=
$
(
element
)
const
href
=
$element
.
attr
(
'href'
)
return
href
&&
href
.
includes
(
'_next/static/chunks/'
)
})
// There should be at least one preload link for dynamic chunks
expect
(
dynamicPreloadLinks
.
length
).
toBeGreaterThan
(
0
)
dynamicPreloadLinks
.
each
((
_
,
element
)
=>
{
const
$element
=
$
(
element
)
const
href
=
$element
.
attr
(
'href'
)
// Only check preload links for dynamic chunks
if
(
href
&&
href
.
includes
(
'_next/static/chunks/'
))
{
expect
(
$element
.
attr
(
'nonce'
)).
toBe
(
'test-nonce'
)
}
})
})
it
(
'should not generate CSP violations when using next/dynamic with nonce'
,
async
()
=>
{
const
browser
=
await
next
.
browser
(
'/'
)
// Wait for the dynamic component to load
await
browser
.
waitForElementByCss
(
'#dynamic-component-loaded'
)
// OPTIMIZATION: Get both text values concurrently
const
[
pageTitle
,
componentText
]
=
await
Promise
.
all
([
browser
.
elementByCss
(
'#page-title'
).
then
((
el
)
=>
el
.
text
()),
browser
.
elementByCss
(
'#dynamic-component-loaded'
).
then
((
el
)
=>
el
.
text
()),
])
expect
(
pageTitle
).
toBe
(
'CSP Nonce Test Page'
)
expect
(
componentText
).
toBe
(
'Dynamic component loaded successfully'
)
// Check for CSP violations in Chrome (most expensive operation)
if
(
global
.
browserName
===
'chrome'
)
{
const
logs
=
await
browser
.
log
()
const
cspViolations
=
logs
.
filter
(
(
log
)
=>
log
.
source
===
'security'
&&
log
.
message
.
includes
(
'Content Security Policy'
)
)
expect
(
cspViolations
).
toEqual
([])
}
})
})
test/e2e/app-dir/next-dynamic-csp-nonce/next.config.js
0 → 100644
View file @
544f5212
/**
* @type {import('next').NextConfig}
*/
const
nextConfig
=
{}
module
.
exports
=
nextConfig
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment