Unverified Commit f1f88e34 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Apply normalizers for init URL used for handlers (#80694)

This applies our normalizers to the `initURL` we pass to the handlers
interface as currently it is created before our existing normalizing
takes place which causes us to fail to generate the correct cache keys
for ISR paths. This specifically breaks our de-duping for client segment
cache as it relies on the cache key being consistent across all segment
paths being requested during a revalidation.

In the future we will have to hoist the URL normalizing anyways so this
leaves it as is until then.

This also adds a regression test for the cache key not being normalized
causing extra render passes for client segment cache entries when it
shouldn't have.
parent b5f26fd9
...@@ -2637,7 +2637,18 @@ export default abstract class Server< ...@@ -2637,7 +2637,18 @@ export default abstract class Server<
const parsedInitUrl = parseUrl( const parsedInitUrl = parseUrl(
getRequestMeta(req, 'initURL') || req.url getRequestMeta(req, 'initURL') || req.url
) )
request.url = `${parsedInitUrl.pathname?.replace(/(\.prefetch\.rsc|\.rsc)$/, '')}${parsedInitUrl.search || ''}` let initPathname = parsedInitUrl.pathname || '/'
for (const normalizer of [
this.normalizers.segmentPrefetchRSC,
this.normalizers.prefetchRSC,
this.normalizers.rsc,
]) {
if (normalizer?.match(initPathname)) {
initPathname = normalizer.normalize(initPathname)
}
}
request.url = `${initPathname}${parsedInitUrl.search || ''}`
// propagate the request context for dev // propagate the request context for dev
setRequestMeta(request, getRequestMeta(req)) setRequestMeta(request, getRequestMeta(req))
......
...@@ -5,6 +5,8 @@ export function generateStaticParams() { ...@@ -5,6 +5,8 @@ export function generateStaticParams() {
} }
export default async function Page({ params }) { export default async function Page({ params }) {
console.log('rendering /isr/[slug]')
const data = await fetch( const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random', 'https://next-data-api-endpoint.vercel.app/api/random',
{ {
......
...@@ -19,6 +19,7 @@ describe('required server files app router', () => { ...@@ -19,6 +19,7 @@ describe('required server files app router', () => {
let appPort: number | string let appPort: number | string
let delayedPostpone let delayedPostpone
let rewritePostpone let rewritePostpone
let cliOutput = ''
const setupNext = async ({ const setupNext = async ({
nextEnv, nextEnv,
...@@ -101,6 +102,12 @@ describe('required server files app router', () => { ...@@ -101,6 +102,12 @@ describe('required server files app router', () => {
undefined, undefined,
{ {
cwd: next.testDir, cwd: next.testDir,
onStderr(data) {
cliOutput += data
},
onStdout(data) {
cliOutput += data
},
} }
) )
} }
...@@ -118,6 +125,38 @@ describe('required server files app router', () => { ...@@ -118,6 +125,38 @@ describe('required server files app router', () => {
expect(next.cliOutput).not.toContain('ERR_INVALID_URL') expect(next.cliOutput).not.toContain('ERR_INVALID_URL')
}) })
// this enables client segment cache in CI
if (process.env.__NEXT_EXPERIMENTAL_PPR) {
it('should de-dupe client segment tree revalidate requests', async () => {
const { segmentPaths } = await next.readJSON(
'standalone/.next/server/app/isr/first.meta'
)
const outputIdx = cliOutput.length
for (const segmentPath of segmentPaths) {
const outputSegmentPath =
join('/isr/[slug].segments', segmentPath) + '.segment.rsc'
require('console').error('requesting', outputSegmentPath)
const res = await fetchViaHTTP(appPort, outputSegmentPath, undefined, {
headers: {
'x-matched-path': '/isr/[slug].segments/_tree.segment.rsc',
'x-now-route-matches': 'slug=first&1=first',
},
})
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toBe('text/x-component')
}
expect(
cliOutput.substring(outputIdx).match(/rendering \/isr\/\[slug\]/g)
.length
).toBe(1)
})
}
it('should properly stream resume with Next-Resume', async () => { it('should properly stream resume with Next-Resume', async () => {
const res = await fetchViaHTTP(appPort, '/delayed', undefined, { const res = await fetchViaHTTP(appPort, '/delayed', undefined, {
headers: { headers: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment