Revert "Emit the variant-prefixed routing entries as one contiguous block"

This reverts commit 89ec44153c6e2c1c9d8c5475c32a383762b0a2fe.
parent e80520ef
...@@ -2188,22 +2188,6 @@ export async function handleBuildComplete({ ...@@ -2188,22 +2188,6 @@ export async function handleBuildComplete({
const dynamicDataRoutes: DynamicRouteItem[] = [] const dynamicDataRoutes: DynamicRouteItem[] = []
const dynamicSegmentRoutes: DynamicRouteItem[] = [] const dynamicSegmentRoutes: DynamicRouteItem[] = []
// The entries that match a request under the prefix of a combination. They
// are collected apart from the entries for the plain paths, and the table
// lists this whole block first.
//
// A root-level catch-all is what forces the order. `/[...slug]` and
// `/[[...slug]]` match `/__variants/<hash>/p1/x`, capturing the whole path
// as the param, so a plain entry listed first would claim a request that
// belongs to a per-combination artifact. This order is safe in both
// directions, because no request for a plain path can match a prefixed
// pattern: every one of them requires the `/__variants/` literal.
//
// The blocks also have to be contiguous. The route collapses group
// neighbours in the table, so a block that a plain entry interrupts cannot
// be collapsed at all.
const variantsPrefixedRoutes: DynamicRouteItem[] = []
const getDestinationQuery = (routeKeys: Record<string, string>) => { const getDestinationQuery = (routeKeys: Record<string, string>) => {
const items = Object.entries(routeKeys ?? {}) const items = Object.entries(routeKeys ?? {})
if (items.length === 0) return '' if (items.length === 0) return ''
...@@ -2445,7 +2429,7 @@ export async function handleBuildComplete({ ...@@ -2445,7 +2429,7 @@ export async function handleBuildComplete({
// form gains nothing of its own here: the prefix is a literal ahead // form gains nothing of its own here: the prefix is a literal ahead
// of the page pattern, so it neither takes part in the suffix group // of the page pattern, so it neither takes part in the suffix group
// nor changes what that group can match. // nor changes what that group can match.
variantsPrefixedRoutes.push({ dynamicRoutes.push({
source: pagePath, source: pagePath,
sourceRegex: variantsSourceRegex.replace( sourceRegex: variantsSourceRegex.replace(
new RegExp(escapeStringRegexp('(?:/)?$')), new RegExp(escapeStringRegexp('(?:/)?$')),
...@@ -2470,7 +2454,7 @@ export async function handleBuildComplete({ ...@@ -2470,7 +2454,7 @@ export async function handleBuildComplete({
// that rule takes any character, and would otherwise take the suffix // that rule takes any character, and would otherwise take the suffix
// as part of the param. // as part of the param.
if (hasAppPages) { if (hasAppPages) {
variantsPrefixedRoutes.push({ dynamicRoutes.push({
source: pagePath + '.rsc', source: pagePath + '.rsc',
sourceRegex: variantsSourceRegex.replace( sourceRegex: variantsSourceRegex.replace(
new RegExp(escapeStringRegexp('(?:/)?$')), new RegExp(escapeStringRegexp('(?:/)?$')),
...@@ -2482,7 +2466,7 @@ export async function handleBuildComplete({ ...@@ -2482,7 +2466,7 @@ export async function handleBuildComplete({
}) })
} }
variantsPrefixedRoutes.push({ dynamicRoutes.push({
source: pagePath, source: pagePath,
sourceRegex: variantsSourceRegex, sourceRegex: variantsSourceRegex,
destination: withCombination(variantsDestination), destination: withCombination(variantsDestination),
...@@ -2534,7 +2518,7 @@ export async function handleBuildComplete({ ...@@ -2534,7 +2518,7 @@ export async function handleBuildComplete({
? path.posix.join('/', config.basePath) ? path.posix.join('/', config.basePath)
: '' : ''
variantsPrefixedRoutes.push({ dynamicRoutes.push({
source: page, source: page,
sourceRegex: `^${basePathPrefix}[/]?/${VARIANTS_PATH_PREFIX}/(?<${NEXT_VARIANTS_QUERY_PARAM}>[^/]+)${escapeStringRegexp(page)}(?:/)?$`, sourceRegex: `^${basePathPrefix}[/]?/${VARIANTS_PATH_PREFIX}/(?<${NEXT_VARIANTS_QUERY_PARAM}>[^/]+)${escapeStringRegexp(page)}(?:/)?$`,
destination: `${path.posix.join('/', config.basePath, page)}?${NEXT_VARIANTS_QUERY_PARAM}=$${NEXT_VARIANTS_QUERY_PARAM}`, destination: `${path.posix.join('/', config.basePath, page)}?${NEXT_VARIANTS_QUERY_PARAM}=$${NEXT_VARIANTS_QUERY_PARAM}`,
...@@ -2643,7 +2627,6 @@ export async function handleBuildComplete({ ...@@ -2643,7 +2627,6 @@ export async function handleBuildComplete({
Log.info(`Running onBuildComplete from ${adapterMod.name}`) Log.info(`Running onBuildComplete from ${adapterMod.name}`)
const combinedDynamicRoutes = [ const combinedDynamicRoutes = [
...variantsPrefixedRoutes,
...dynamicDataRoutes, ...dynamicDataRoutes,
...dynamicSegmentRoutes, ...dynamicSegmentRoutes,
...dynamicRoutes, ...dynamicRoutes,
......
...@@ -175,51 +175,20 @@ describe('adapter-variants', () => { ...@@ -175,51 +175,20 @@ describe('adapter-variants', () => {
.toMatchInlineSnapshot(` .toMatchInlineSnapshot(`
"3 entries "3 entries
/dynamic/[slug]
^[/]?/dynamic/(?<nxtPslug>[^/]+?)(?<rscSuffix>\\.rsc|\\.segments/.+\\.segment\\.rsc|)(?:/)?$
-> /dynamic/[slug]$rscSuffix?nxtPslug=$nxtPslug
/dynamic/[slug] /dynamic/[slug]
^[/]?/__variants/(?<nxtV>[^/]+)/dynamic/(?<nxtPslug>[^/]+?)(?<rscSuffix>\\.rsc|\\.segments/.+\\.segment\\.rsc|)(?:/)?$ ^[/]?/__variants/(?<nxtV>[^/]+)/dynamic/(?<nxtPslug>[^/]+?)(?<rscSuffix>\\.rsc|\\.segments/.+\\.segment\\.rsc|)(?:/)?$
-> /__variants/$nxtV/dynamic/[slug]$rscSuffix?nxtPslug=$nxtPslug&nxtV=$nxtV -> /__variants/$nxtV/dynamic/[slug]$rscSuffix?nxtPslug=$nxtPslug&nxtV=$nxtV
/concrete /concrete
^[/]?/__variants/(?<nxtV>[^/]+)/concrete(?:/)?$ ^[/]?/__variants/(?<nxtV>[^/]+)/concrete(?:/)?$
-> /concrete?nxtV=$nxtV -> /concrete?nxtV=$nxtV"
/dynamic/[slug]
^[/]?/dynamic/(?<nxtPslug>[^/]+?)(?<rscSuffix>\\.rsc|\\.segments/.+\\.segment\\.rsc|)(?:/)?$
-> /dynamic/[slug]$rscSuffix?nxtPslug=$nxtPslug"
`) `)
}) })
it('should list every prefixed entry before every plain one', () => {
// A route collapse groups entries that are neighbours in the table, so the
// entries that could collapse together have to be contiguous. One pair per
// page, plain then prefixed, interleaves the two kinds and leaves neither
// kind contiguous, which is why the build emits them as two blocks.
//
// The prefixed block comes first, and the reverse order is unsafe. A
// root-level catch-all matches a prefixed path and captures the whole of it
// as its param, so a plain entry ahead of the prefixed block would claim a
// request that belongs to a per-combination artifact. This order is safe in
// both directions, because no request for a plain path can match a prefixed
// pattern: every one of those requires the `/__variants/` literal.
//
// This order changes nothing on its own. It is what lets a later cross-page
// collapse group these entries at all.
for (const context of [fourCombinations, collapsed]) {
const kinds = context.routing.dynamicRoutes.map((route) =>
route.sourceRegex.includes('/__variants/') ? 'prefixed' : 'plain'
)
// The fixture has to produce both kinds, or the assertion below holds for
// a reason that has nothing to do with the order.
expect(kinds).toContain('prefixed')
expect(kinds).toContain('plain')
expect(kinds.indexOf('plain')).toBeGreaterThan(
kinds.lastIndexOf('prefixed')
)
}
})
it('should write the same prerenders whether the collapse is on or off', () => { it('should write the same prerenders whether the collapse is on or off', () => {
// The option rewrites the routing table and nothing else. The artifacts and // The option rewrites the routing table and nothing else. The artifacts and
// their paths carry the per-combination cache keys, so a collapse that // their paths carry the per-combination cache keys, so a collapse that
......
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