perf: skip rewrite processing when no rewrites configured#91597
Open
benfavre wants to merge 1 commit intovercel:canaryfrom
Open
perf: skip rewrite processing when no rewrites configured#91597benfavre wants to merge 1 commit intovercel:canaryfrom
benfavre wants to merge 1 commit intovercel:canaryfrom
Conversation
For Next.js apps that don't configure rewrites (the common case), `handleRewrites` was performing a `structuredClone` of the parsed URL, creating `matchesPage` and `checkRewrite` closures, and iterating three empty arrays on every request — only to return the clone unchanged. This adds a fast path that precomputes a `hasRewrites` flag once in `getServerUtils` and returns the original `parsedUrl` immediately when no rewrites exist, avoiding the deep clone, closure allocation, and iteration overhead per request. Also exposes the `hasRewrites` flag on the returned utils object so callers can make rewrite-aware decisions if needed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
Contributor
Author
Performance ImpactProfiling setup: Node.js v25.7.0, Before (canary):
After (this PR):
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
handleRewrites(inserver-utils.ts) that returns the originalparsedUrlimmediately when no rewrites are configured, avoiding a per-requeststructuredClone, closure creation, and empty array iterationhasRewritesflag once ingetServerUtils(at route setup time) rather than checking array lengths on every requesthasRewriteson the returned utils object for caller awarenessWhy
For Next.js apps that don't use rewrites (the common case), every request through the minimal-mode code path in
base-server.tsand theroute-module.tsrender path calledhandleRewrites, which:structuredClone(parsedUrl)— a deep clone of the URL + query objectmatchesPageandcheckRewriteclosures (allocating two function objects)beforeFiles,afterFiles,fallback)With this change, when all three rewrite arrays are empty (or undefined), the function returns immediately with the original
parsedUrlreference and an emptyrewriteParamsobject. This is safe because:base-server.tsonly reads fromrewrittenParsedUrl(to createrewrittenQueryParamscopy and checkdidRewrite)route-module.tslineObject.assign(parsedUrl.query, rewrittenParsedUrl.query)becomes a self-assign no-opnormalizeLocalePathonrewrittenParsedUrl.pathnameis idempotent when it equalsparsedUrl.pathnameTest plan
tsc --noEmitonpackages/next— 0 new errors)server-utils.test.tstests all userewrites: {}(empty config), exercising the new fast path