mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #258] [MERGED] refactor: extract shared request handling from server entry points #422
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#422
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/cloudflare/vinext/pull/258
Author: @yunus25jmi1
Created: 3/5/2026
Status: ✅ Merged
Merged: 3/9/2026
Merged by: @james-elicx
Base:
main← Head:refactor/shared-request-handling📝 Commits (8)
b59724drefactor: extract shared request handling into config-matchers and request-pipelinebd15f7dfix: address bonk review - remove dead applyTo param, dangling comment44139b0Merge remote-tracking branch 'upstream/main' into refactor/shared-request-handling48e4924fix: remove dangling comments (parseCookies stub, sanitizeDestination stub)3632312fix: remove bare block around __tsRedirect, add multiple trailing slash testa2d7ec7fix: stricter /api prefix check for trailing slash normalization (matches only /api or /api/), add tests for edge casesd41dcb3Merge remote-tracking branch 'origin/main' into refactor/shared-request-handling83f48b6test: update entry-template snapshots after merging main📊 Changes
8 files changed (+777 additions, -2404 deletions)
View changed files
📝
packages/vinext/src/deploy.ts(+1 -1)📝
packages/vinext/src/index.ts(+19 -188)📝
packages/vinext/src/server/app-dev-server.ts(+41 -317)📝
packages/vinext/src/server/prod-server.ts(+1 -1)➕
packages/vinext/src/server/request-pipeline.ts(+224 -0)📝
tests/__snapshots__/entry-templates.test.ts.snap(+201 -1867)📝
tests/app-router.test.ts(+28 -30)➕
tests/request-pipeline.test.ts(+262 -0)📄 Description
Summary
Deduplicates ~250 lines of inline request-handling logic from the generated App Router RSC entry (
app-dev-server.ts) and Pages Router server entry (index.ts) by importing from the canonicalconfig-matchers.tsand a newrequest-pipeline.tsmodule.Changes
New file:
server/request-pipeline.tsShared request lifecycle utilities extracted for reuse across entry points:
guardProtocolRelativeUrl()— blocks//evil.comprotocol-relative URL attacksstripBasePath()— removes configured basePath prefix from pathnamesnormalizeTrailingSlash()— redirects to canonical trailing-slash formvalidateCsrfOrigin()— CSRF origin validation for server actionsisOriginAllowed()— wildcard subdomain matching for allowed originsvalidateImageUrl()— image optimization URL validationprocessMiddlewareHeaders()— strips internalx-middleware-*headersapp-dev-server.ts(~250 lines removed)Replaced 14 inline
__-prefixed functions with imports fromconfig-matchers.tsandrequest-pipeline.ts:matchRedirect,matchRewrite,matchHeaders,sanitizeDestinationisExternalUrl,proxyExternalRequestrequestContextFromRequestvalidateCsrfOrigin,validateImageUrlindex.tsparseCookiesin generated server entry template with import fromconfig-matchers.tsRequestContextconstruction withrequestContextFromRequest()sanitizeDestinationLocal(), using importedsanitizeDestination()matchConfigPattern+extractConstraint(~100 lines), re-exporting fromconfig-matchers.tsTest updates
app-router.test.tsassertions to reference the new imported function names instead of old inline__-prefixed names.request-pipeline.test.tswith 33 unit tests covering all 6 exported functions.Behavioral Change
normalizeTrailingSlashnow uses relativeLocationheaders instead of absolute URLs. The old inline code usedResponse.redirect(new URL(..., request.url), 308)which produces an absoluteLocationheader; the new code usesnew Response(null, { status: 308, headers: { Location: relativePath } }). This is valid per RFC 7231 §7.1.2 and aligns dev behavior withprod-server.ts(which already uses relativeLocationheaders).Security Fixes
.replace()divergence (CodeQL flagged incomplete sanitization) in favor of config-matchers single-pass tokenizerAbortController+setTimeout(broader runtime support) instead ofAbortSignal.timeout().replace()Verification
pnpm run typecheckpasses cleanCloses #254
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.