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
092df0f1
Commit
092df0f1
authored
Jul 29, 2025
by
Janka Uryga
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support experimental.validateRSCRequestHeaders for runtime prefetches
parent
63e373bf
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
240 additions
and
107 deletions
+240
-107
page.tsx
.../e2e/app-dir/segment-cache/cdn-cache-busting/app/page.tsx
+32
-10
cdn-cache-busting.test.ts
...segment-cache/cdn-cache-busting/cdn-cache-busting.test.ts
+192
-85
server.mjs
test/e2e/app-dir/segment-cache/cdn-cache-busting/server.mjs
+8
-4
router-act.ts
test/e2e/app-dir/segment-cache/router-act.ts
+8
-8
No files found.
test/e2e/app-dir/segment-cache/cdn-cache-busting/app/page.tsx
View file @
092df0f1
...
...
@@ -2,15 +2,37 @@ import { LinkAccordion } from '../components/link-accordion'
export
default
function
Page
()
{
return
(
<
ul
>
<
li
>
<
LinkAccordion
href=
"/target-page"
>
Target page
</
LinkAccordion
>
</
li
>
<
li
>
<
LinkAccordion
href=
"/redirect-to-target-page"
>
Redirects to target page
</
LinkAccordion
>
</
li
>
</
ul
>
<
div
>
<
h2
>
auto prefetches
</
h2
>
<
ul
id=
"prefetch-auto"
>
<
li
>
<
LinkAccordion
href=
"/target-page"
>
Target page
</
LinkAccordion
>
</
li
>
<
li
>
<
LinkAccordion
href=
"/redirect-to-target-page"
>
Redirects to target page
</
LinkAccordion
>
</
li
>
</
ul
>
{
process
.
env
.
__NEXT_CACHE_COMPONENTS
&&
(
// runtime prefetches are only available if cacheComponents is enabled.
<>
<
h2
>
runtime prefetches
</
h2
>
<
ul
id=
"prefetch-runtime"
>
<
li
>
<
LinkAccordion
href=
"/target-page"
prefetch=
{
true
}
>
Target page
</
LinkAccordion
>
</
li
>
<
li
>
<
LinkAccordion
href=
"/redirect-to-target-page"
prefetch=
{
true
}
>
Redirects to target page
</
LinkAccordion
>
</
li
>
</
ul
>
</>
)
}
</
div
>
)
}
test/e2e/app-dir/segment-cache/cdn-cache-busting/cdn-cache-busting.test.ts
View file @
092df0f1
...
...
@@ -5,14 +5,15 @@ import { findPort, nextBuild } from 'next-test-utils'
import
{
isNextDeploy
,
isNextDev
}
from
'e2e-utils'
import
{
start
}
from
'./server.mjs'
const
isCacheComponentsEnabled
=
process
.
env
.
__NEXT_EXPERIMENTAL_CACHE_COMPONENTS
===
'true'
describe
(
'segment cache (CDN cache busting)'
,
()
=>
{
if
(
isNextDev
||
isNextDeploy
)
{
test
(
'should not run during dev or deploy test runs'
,
()
=>
{})
return
}
// TODO(runtime-ppr): add tests for runtime prefetches
// To debug these tests locally, run:
// node start.mjs
//
...
...
@@ -34,110 +35,216 @@ describe('segment cache (CDN cache busting)', () => {
await
cleanup
()
})
it
(
describe
(
"perform fully prefetched navigation with a CDN that doesn't respect "
+
'the Vary header'
,
async
()
=>
{
let
act
const
browser
=
await
webdriver
(
port
,
'/'
,
{
beforePageLoad
(
p
:
Playwright
.
Page
)
{
act
=
createRouterAct
(
p
)
},
()
=>
{
it
(
'static prefetch'
,
async
()
=>
{
let
act
const
browser
=
await
webdriver
(
port
,
'/'
,
{
beforePageLoad
(
p
:
Playwright
.
Page
)
{
act
=
createRouterAct
(
p
)
},
})
// Initiate a prefetch. Each segment will be prefetched individually,
// using the pathname of the target page and a custom header specifying
// the segment. If we didn't also set a cache-busting search param, then
// the fake CDN used by this test suite would incorrectly use the same
// entry for every segment, poisoning the cache.
await
act
(
async
()
=>
{
const
linkToggle
=
await
browser
.
elementByCss
(
'#prefetch-auto [data-link-accordion="/target-page"]'
)
await
linkToggle
.
click
()
},
{
includes
:
'Target page'
,
}
)
// Navigate to the prefetched target page.
await
act
(
async
()
=>
{
const
link
=
await
browser
.
elementByCss
(
'a[href="/target-page"]'
)
await
link
.
click
()
// The page was prefetched, so we're able to render the target
// page immediately.
const
div
=
await
browser
.
elementById
(
'target-page'
)
expect
(
await
div
.
text
()).
toBe
(
'Target page'
)
},
'no-requests'
)
})
// Initiate a prefetch. Each segment will be prefetched individually,
// using the pathname of the target page and a custom header specifying
// the segment. If we didn't also set a cache-busting search param, then
// the fake CDN used by this test suite would incorrectly use the same
// entry for every segment, poisoning the cache.
await
act
(
async
()
=>
{
const
linkToggle
=
await
browser
.
elementByCss
(
'[data-link-accordion="/target-page"]'
if
(
isCacheComponentsEnabled
)
{
it
(
'runtime prefetch'
,
async
()
=>
{
let
act
const
browser
=
await
webdriver
(
port
,
'/'
,
{
beforePageLoad
(
p
:
Playwright
.
Page
)
{
act
=
createRouterAct
(
p
)
},
})
// Initiate a prefetch. We'll send two requests - one to prefetch the tree,
// and another to prefetch the page content (which is static, so it will be complete).
// If we didn't also set a cache-busting search param, then
// the fake CDN used by this test suite would incorrectly use the same
// entry for both responses, poisoning the cache.
await
act
(
async
()
=>
{
const
linkToggle
=
await
browser
.
elementByCss
(
'#prefetch-runtime [data-link-accordion="/target-page"]'
)
await
linkToggle
.
click
()
},
{
// This should be returned as part of the second request, if it wasn't cache poisoned.
includes
:
'Target page'
,
}
)
await
linkToggle
.
click
()
},
{
includes
:
'Target page'
,
}
)
// Navigate to the prefetched target page.
await
act
(
async
()
=>
{
const
link
=
await
browser
.
elementByCss
(
'a[href="/target-page"]'
)
await
link
.
click
()
// The page was prefetched, so we're able to render the target
// page immediately.
const
div
=
await
browser
.
elementById
(
'target-page'
)
expect
(
await
div
.
text
()).
toBe
(
'Target page'
)
},
'no-requests'
)
// Navigate to the prefetched target page.
await
act
(
async
()
=>
{
const
link
=
await
browser
.
elementByCss
(
'a[href="/target-page"]'
)
await
link
.
click
()
// The page was prefetched, so we're able to render the target
// page immediately.
const
div
=
await
browser
.
elementById
(
'target-page'
)
expect
(
await
div
.
text
()).
toBe
(
'Target page'
)
},
'no-requests'
)
})
}
}
)
it
(
describe
(
'prevent cache poisoning attacks by responding with a redirect to correct '
+
'cache busting query param if a custom header is sent during a prefetch '
+
'without a corresponding cache-busting search param'
,
async
()
=>
{
const
browser
=
await
webdriver
(
port
,
'/'
)
const
{
status
,
responseUrl
,
redirected
}
=
await
browser
.
eval
(
async
()
=>
{
const
res
=
await
fetch
(
'/target-page'
,
{
headers
:
{
rsc
:
'1'
,
'next-router-prefetch'
:
'1'
,
'next-router-segment-prefetch'
:
'/_tree'
,
},
})
return
{
status
:
res
.
status
,
responseUrl
:
res
.
url
,
redirected
:
res
.
redirected
,
()
=>
{
it
(
'static prefetch'
,
async
()
=>
{
const
browser
=
await
webdriver
(
port
,
'/'
)
const
{
status
,
responseUrl
,
redirected
}
=
await
browser
.
eval
(
async
()
=>
{
const
res
=
await
fetch
(
'/target-page'
,
{
headers
:
{
rsc
:
'1'
,
'next-router-prefetch'
:
'1'
,
'next-router-segment-prefetch'
:
'/_tree'
,
},
})
return
{
status
:
res
.
status
,
responseUrl
:
res
.
url
,
redirected
:
res
.
redirected
,
}
}
}
)
expect
(
status
).
toBe
(
200
)
expect
(
responseUrl
).
toContain
(
'_rsc='
)
expect
(
redirected
).
toBe
(
true
)
)
expect
(
status
).
toBe
(
200
)
expect
(
responseUrl
).
toContain
(
'_rsc='
)
expect
(
redirected
).
toBe
(
true
)
})
if
(
isCacheComponentsEnabled
)
{
it
(
'runtime prefetch'
,
async
()
=>
{
const
browser
=
await
webdriver
(
port
,
'/'
)
const
{
status
,
responseUrl
,
redirected
}
=
await
browser
.
eval
(
async
()
=>
{
const
res
=
await
fetch
(
'/target-page'
,
{
headers
:
{
rsc
:
'1'
,
'next-router-prefetch'
:
'2'
,
},
})
return
{
status
:
res
.
status
,
responseUrl
:
res
.
url
,
redirected
:
res
.
redirected
,
}
}
)
expect
(
status
).
toBe
(
200
)
expect
(
responseUrl
).
toContain
(
'_rsc='
)
expect
(
redirected
).
toBe
(
true
)
})
}
}
)
it
(
describe
(
'perform fully prefetched navigation when a third-party proxy '
+
'performs a redirect'
,
async
()
=>
{
let
act
const
browser
=
await
webdriver
(
port
,
'/'
,
{
beforePageLoad
(
p
:
Playwright
.
Page
)
{
act
=
createRouterAct
(
p
)
},
()
=>
{
it
(
'static prefetch'
,
async
()
=>
{
let
act
const
browser
=
await
webdriver
(
port
,
'/'
,
{
beforePageLoad
(
p
:
Playwright
.
Page
)
{
act
=
createRouterAct
(
p
)
},
})
await
act
(
async
()
=>
{
const
linkToggle
=
await
browser
.
elementByCss
(
'#prefetch-auto [data-link-accordion="/redirect-to-target-page"]'
)
await
linkToggle
.
click
()
},
{
includes
:
'Target page'
,
}
)
// Navigate to the prefetched target page.
await
act
(
async
()
=>
{
const
link
=
await
browser
.
elementByCss
(
'a[href="/redirect-to-target-page"]'
)
await
link
.
click
()
// The page was prefetched, so we're able to render the target
// page immediately.
const
div
=
await
browser
.
elementById
(
'target-page'
)
expect
(
await
div
.
text
()).
toBe
(
'Target page'
)
},
'no-requests'
)
})
await
act
(
async
()
=>
{
const
linkToggle
=
await
browser
.
elementByCss
(
'[data-link-accordion="/redirect-to-target-page"]'
if
(
isCacheComponentsEnabled
)
{
it
(
'runtime prefetch'
,
async
()
=>
{
let
act
const
browser
=
await
webdriver
(
port
,
'/'
,
{
beforePageLoad
(
p
:
Playwright
.
Page
)
{
act
=
createRouterAct
(
p
)
},
})
await
act
(
async
()
=>
{
const
linkToggle
=
await
browser
.
elementByCss
(
'#prefetch-runtime [data-link-accordion="/redirect-to-target-page"]'
)
await
linkToggle
.
click
()
},
{
includes
:
'Target page'
,
}
)
await
linkToggle
.
click
()
},
{
includes
:
'Target page'
,
}
)
// Navigate to the prefetched target page.
await
act
(
async
()
=>
{
const
link
=
await
browser
.
elementByCss
(
'a[href="/redirect-to-target-page"]'
)
await
link
.
click
()
// The page was prefetched, so we're able to render the target
// page immediately.
const
div
=
await
browser
.
elementById
(
'target-page'
)
expect
(
await
div
.
text
()).
toBe
(
'Target page'
)
},
'no-requests'
)
// Navigate to the prefetched target page.
await
act
(
async
()
=>
{
const
link
=
await
browser
.
elementByCss
(
'a[href="/redirect-to-target-page"]'
)
await
link
.
click
()
// The page was prefetched, so we're able to render the target
// page immediately.
const
div
=
await
browser
.
elementById
(
'target-page'
)
expect
(
await
div
.
text
()).
toBe
(
'Target page'
)
},
'no-requests'
)
})
}
}
)
})
test/e2e/app-dir/segment-cache/cdn-cache-busting/server.mjs
View file @
092df0f1
...
...
@@ -135,11 +135,15 @@ async function createFakeCDN(destPort) {
}
resolveCDNEntry
(
entry
)
})
return
}
else
{
// If the response isn't cacheable, pipe it through to the client.
res
.
writeHead
(
proxyRes
.
statusCode
||
200
,
proxyRes
.
statusMessage
,
proxyRes
.
headers
)
proxyRes
.
pipe
(
res
)
}
// If the response isn't cacheable, pipe it through to the client.
proxyRes
.
pipe
(
res
)
return
})
return
cdnServer
...
...
test/e2e/app-dir/segment-cache/router-act.ts
View file @
092df0f1
...
...
@@ -159,7 +159,7 @@ export function createRouterAct(
// `act` controls the timing of when responses reach the client,
// but it should not affect the timing of when requests reach the
// server; we pass the request to the server the immediately.
result
:
new
Promise
(
async
(
resolve
)
=>
{
result
:
(
async
(
)
=>
{
const
originalResponse
=
await
page
.
request
.
fetch
(
request
,
{
maxRedirects
:
0
,
})
...
...
@@ -172,13 +172,13 @@ export function createRouterAct(
const
headers
=
originalResponse
.
headers
()
delete
headers
[
'transfer-encoding'
]
re
solve
(
{
re
turn
{
text
:
await
originalResponse
.
text
(),
body
:
await
originalResponse
.
body
(),
headers
,
status
:
originalResponse
.
status
(),
}
)
}),
}
})
()
,
didProcess
:
false
,
})
if
(
onDidIssueFirstRequest
!==
null
)
{
...
...
@@ -417,14 +417,14 @@ ${fulfilled.body}
batch
.
pendingRequests
.
add
({
url
:
req
.
url
(),
route
:
null
,
result
:
new
Promise
(
async
(
resolve
)
=>
{
re
solve
(
{
result
:
(
async
(
)
=>
{
re
turn
{
text
:
await
res
.
text
(),
body
:
await
res
.
body
(),
headers
:
res
.
headers
(),
status
:
res
.
status
(),
}
)
}),
}
})
()
,
didProcess
:
false
,
})
page
.
off
(
'response'
,
handleResponse
)
...
...
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