Unverified Commit b9be0685 authored by Josh Story's avatar Josh Story Committed by GitHub

Avoid decoding prerender matcher parameters twice (#99121)

## Summary

Avoid decoding route parameters a second time while selecting prerender
metadata. This fixes valid percent-containing URLs returning 500,
including both build-generated closed routes and runtime-generated open
routes.

For example, a request for
`/open/docs/space%20here/with%2Fslash/fallback%25` is already normalized
for manifest lookup before reaching `PrerenderManifestMatcher`. Its
final parameter now contains a literal `%`. The matcher previously used
`getRouteMatcher`, which tried to decode the captures again and threw
even though this call only needed to determine whether the route
structure matched.

Test the existing, lazily constructed route regular expression directly
instead. Matcher precedence, source-page filtering, and the current
server parameter encoding behavior stay unchanged. This does not
introduce a new manifest or adapter contract.

The open fallback is requested twice to cover generation and reuse. The
existing fixture uses `dynamicParams = false` and remains excluded from
the Cache Components matrix, which does not support that configuration.

This is an independent prerequisite for #97393: the failure reproduces
without the parameter-matching API and should be fixed for existing apps
too.

## Verification

- Before the fix, the new closed and open catch-all requests returned
500 with a parameter-decoding error. After the fix, both return 200 and
retain the existing encoded server parameter values.
- The focused matcher unit suite passes all six tests.
- The three encoding regressions pass with production Webpack after
restacking. The API routing suite also passes all 36 tests with
fresh-native production Turbopack above this prerequisite, including its
existing percent-containing URL cases.
- The earlier isolated verification passed package TypeScript and
changed-file lint. Deployment verification remains for CI.

<!-- NEXT_JS_LLM -->
parent c1ce6ea2
......@@ -145,6 +145,28 @@ describe('PrerenderManifestMatcher', () => {
route: genericRootRoute,
})
})
it('should match a pathname that was already decoded for manifest lookup', () => {
const route = createMockDynamicRoute({
fallbackSourceRoute: '/open/[...slug]',
})
const manifest = createMockPrerenderManifest({
'/open/[...slug]': route,
})
const matcher = new PrerenderManifestMatcher(
'/open/[...slug]',
manifest
)
expect(
matcher.match('/open/docs/space here/with%2Fslash/100%')
).toEqual({
source: '/open/[...slug]',
route,
})
})
})
describe('no match scenarios', () => {
......
......@@ -3,10 +3,6 @@ import type {
PrerenderManifest,
} from '../../../../build'
import type { DeepReadonly } from '../../../../shared/lib/deep-readonly'
import {
getRouteMatcher,
type RouteMatchFn,
} from '../../../../shared/lib/router/utils/route-matcher'
import { getRouteRegex } from '../../../../shared/lib/router/utils/route-regex'
/**
......@@ -17,7 +13,7 @@ type Matcher = {
* The matcher for the dynamic route. This is lazily created when the matcher
* is first used.
*/
matcher?: RouteMatchFn
matcher?: RegExp
/**
* The source of the dynamic route.
......@@ -69,11 +65,13 @@ export class PrerenderManifestMatcher {
for (const matcher of this.matchers) {
// Lazily create the matcher, this is only done once per matcher.
if (!matcher.matcher) {
matcher.matcher = getRouteMatcher(getRouteRegex(matcher.source))
// The pathname has already been decoded for manifest lookup. We only
// need to check its structure here; extracting captures would decode
// route parameters again and reject literal percent signs.
matcher.matcher = getRouteRegex(matcher.source).re
}
const match = matcher.matcher(pathname)
if (match) {
if (matcher.matcher.test(pathname)) {
return {
source: matcher.source,
route: matcher.route,
......
export const dynamicParams = false
export function generateStaticParams() {
return [
{ slug: ['docs', 'space here', '100%'] },
{ slug: ['docs', 'space here', 'with/slash', '100%'] },
]
}
export default async function Page({
params,
}: {
params: Promise<{ slug: string[] }>
}) {
const { slug } = await params
return <div>params.slug is {slug.join('/')}</div>
}
export function generateStaticParams() {
return [
{ slug: ['docs', 'space here', '100%'] },
{ slug: ['docs', 'space here', 'with/slash', '100%'] },
]
}
export default async function Page({
params,
}: {
params: Promise<{ slug: string[] }>
}) {
const { slug } = await params
return <div>params.slug is {slug.join('/')}</div>
}
import { load } from 'cheerio'
import { nextTestSetup } from 'e2e-utils'
describe('prerender-encoding', () => {
......@@ -9,4 +10,52 @@ describe('prerender-encoding', () => {
const $ = await next.render$('/sticks%20%26%20stones')
expect($('div').text()).toBe('params.id is sticks%20%26%20stones')
})
it('should serve an exact closed catch-all path containing a literal percent', async () => {
const response = await next.fetch('/closed/docs/space%20here/100%25')
expect(response.status).toBe(200)
const $ = load(await response.text())
expect($('div').text()).toBe('params.slug is docs/space%20here/100%25')
})
it('should serve an open catch-all fallback containing a literal percent', async () => {
const pathname = '/open/docs/space%20here/fallback%25'
for (let i = 0; i < 2; i++) {
const response = await next.fetch(pathname)
expect(response.status).toBe(200)
const $ = load(await response.text())
expect($('div').text()).toBe(
'params.slug is docs/space%20here/fallback%25'
)
}
})
// Vercel tries the raw URL and the fully decoded path for filesystem lookup.
// Neither matches this output's decoded space/percent and still-encoded slash.
// @gate !deploy
it('should preserve an encoded slash in an exact closed catch-all path', async () => {
const response = await next.fetch(
'/closed/docs/space%20here/with%2Fslash/100%25'
)
expect(response.status).toBe(200)
const $ = load(await response.text())
expect($('div').text()).toBe(
'params.slug is docs/space%20here/with%2Fslash/100%25'
)
})
// Vercel decodes the captured slash before splitting catch-all values, whereas
// next start preserves it within a single value.
// @gate !deploy
it('should preserve an encoded slash in an open catch-all fallback', async () => {
const pathname = '/open/docs/space%20here/with%2Fslash/fallback%25'
for (let i = 0; i < 2; i++) {
const response = await next.fetch(pathname)
expect(response.status).toBe(200)
const $ = load(await response.text())
expect($('div').text()).toBe(
'params.slug is docs/space%20here/with%2Fslash/fallback%25'
)
}
})
})
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