Skip to content

Commit 8e93b20

Browse files
authored
Development: Skip route matching when there is an existing match (#84227)
## What? Adds a fast path when there is an existing match already from matching earlier in the request lifecycle. This works for the majority of cases except for Parallel Routes as it will end up finding the wrong bundle path. That is fundamentally related to the implementation of parallel routes and where the bundles are created (under the specific parallel route name). This will need a larger refactor to fix but that doesn't have to block landing the fast path for all other cases.
1 parent 962c2c3 commit 8e93b20

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

packages/next/src/server/base-server.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,11 +2558,30 @@ export default abstract class Server<
25582558
i18n: this.i18nProvider?.fromRequest(req, pathname),
25592559
}
25602560

2561+
const existingMatch = getRequestMeta(ctx.req, 'match')
2562+
2563+
let fastPath = true
2564+
// when a specific invoke-output is meant to be matched
2565+
// ensure a prior dynamic route/page doesn't take priority
2566+
const invokeOutput = getRequestMeta(ctx.req, 'invokeOutput')
2567+
2568+
if (
2569+
(!this.minimalMode &&
2570+
typeof invokeOutput === 'string' &&
2571+
isDynamicRoute(invokeOutput || '') &&
2572+
invokeOutput !== existingMatch?.definition.pathname) ||
2573+
// Parallel routes are matched in `existingMatch` but since currently
2574+
// there can be multiple matches it's not guaranteed to be the right match
2575+
// therefor we need to opt-out of the fast path for parallel routes.
2576+
existingMatch?.definition.page.includes('/@')
2577+
) {
2578+
fastPath = false
2579+
}
2580+
25612581
try {
2562-
for await (const match of this.matchers.matchAll(pathname, options)) {
2563-
// when a specific invoke-output is meant to be matched
2564-
// ensure a prior dynamic route/page doesn't take priority
2565-
const invokeOutput = getRequestMeta(ctx.req, 'invokeOutput')
2582+
for await (const match of fastPath && existingMatch
2583+
? [existingMatch]
2584+
: this.matchers.matchAll(pathname, options)) {
25662585
if (
25672586
!this.minimalMode &&
25682587
typeof invokeOutput === 'string' &&

0 commit comments

Comments
 (0)