Skip to content

Fix Next.js locale matching and Pages Router request routing - #394

Open
soleil-colza wants to merge 7 commits into
mainfrom
fix/locale-middleware-budget-20261029
Open

soleil-colza wants to merge 7 commits into
mainfrom
fix/locale-middleware-budget-20261029

Conversation

@soleil-colza

@soleil-colza soleil-colza commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fix three gaps in Next.js locale routing: ordinary locale-aware middleware paths could exceed the request pattern budget, Pages Router config rewrites could lose their added query under basePath, and root locale negotiation differed from Next.js.

  • Reuse canonical locale decomposition within the existing request budget. Judge lookahead on the original pathname, prove the complete derived grammar, cache bounded patterns, and retain the JSON-only suffix grammar of Next.js 16.2.0.
  • Pass the effective Pages rewrite query through Next.js's existing request metadata and validated internal handoff. Preserve the source URL, asPath and resolvedUrl; keep middleware's documented deployed-query behavior.
  • Match Next.js's domain, language and first-cookie negotiation once at the original root, before user middleware. Disable duplicate negotiation in the internal router while retaining locale normalization. Preserve minimal trusted locale inputs across edge middleware rewrites, header overrides, continuations and anonymous image-source requests.
  • Strip forged carriers and keep them out of handlers and external requests. Preserve cached target paths and cache static configuration and parsed patterns. Add no network operation, asynchronous wait, body buffering, manifest field or application setting.

Validation: focused matcher contracts pass on Next.js 16.4.3 (41 tests) and exact 16.2.0 (39 passed, two modern segment-payload cases skipped). Thirteen locale oracle/provenance cases pass on both versions. Fourteen query/handoff and 16 metadata cases pass, alongside focused local workerd regressions for real routing, edge handoff, spoofing, cache paths and image sources. The final real-router locale/slash suite passes 46 cases. Changed files pass scoped lint and formatting; full gates and exact-floor contracts run in CI. No benchmark or Cloudflare deployment.

Final commit 26f0f20 passes all required CI gates, including aggregate check, lint, tests, type/static checks and the local stack. Codex and Greptile reviewed that commit with no findings. CodeRabbit reported no actionable comments through e339274; its final incremental review is unavailable because the hourly review allowance is exhausted.

@arkor-deploy

arkor-deploy Bot commented Oct 29, 2026

Copy link
Copy Markdown

Arkor — Preview deployments for arkorlab-upwind

Use the checkbox below to choose whether this pull request gets preview deployments.

  • Build and deploy previews for this pull request

Checking this builds the latest commit and future pushes. Unchecking stops queued and future builds; a running build can finish and an existing preview stays available.

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 49 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 896fe3bb-0e96-45c7-92cf-b800b740956d
Commits

Reviewing files that changed from the base of the PR and between e339274 and 26f0f20.

Files selected for processing: 2
  • packages/runtime/src/routing.ts
  • packages/runtime/test/workers/locale-slash-redirects.test.ts

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 84f3e38f-b267-4da4-878e-bf22d6934dbb
Commits

Reviewing files that changed from the base of the PR and between 4410822 and e339274.

Files selected for processing: 38
  • .github/workflows/ci.yaml
  • packages/adapter/test/build-middleware-budget.test.ts
  • packages/adapter/test/payload-matcher.test.ts
  • packages/core/src/bundle/pattern-budget.ts
  • packages/core/src/paas/index.ts
  • packages/core/src/paas/locale-request.ts
  • packages/core/src/paas/middleware.ts
  • packages/core/src/paas/payload-matcher.ts
  • packages/core/src/paas/protocol.ts
  • packages/core/src/request/middleware-locale.ts
  • packages/core/src/request/pattern-budget.ts
  • packages/core/test/locale-request.test.ts
  • packages/edge/src/handlers/document-upstream.ts
  • packages/edge/src/handlers/image-source-rewrite.ts
  • packages/edge/src/handlers/image-source.ts
  • packages/edge/src/handlers/locale-request.ts
  • packages/edge/src/handlers/middleware.ts
  • packages/edge/src/handlers/passthrough.ts
  • packages/edge/src/route.ts
  • packages/edge/src/strategies/context.ts
  • packages/edge/src/strategies/continuation-resume.ts
  • packages/edge/test/image-source-routing.test.ts
  • packages/edge/test/locale-request.test.ts
  • packages/runtime/NOTICE
  • packages/runtime/src/handle.ts
  • packages/runtime/src/handoff.ts
  • packages/runtime/src/incoming.ts
  • packages/runtime/src/locale-language.ts
  • packages/runtime/src/locale-redirect.ts
  • packages/runtime/src/middleware-invoke.ts
  • packages/runtime/src/outputs.ts
  • packages/runtime/src/resolve-request.ts
  • packages/runtime/src/routing.ts
  • packages/runtime/src/serve.ts
  • packages/runtime/src/source-input.ts
  • packages/runtime/test/handoff.test.ts
  • packages/runtime/test/locale-redirect.test.ts
  • packages/runtime/test/pages-rewrite-query.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

The pull request adds Next.js middleware matcher compatibility, carries original locale inputs through edge and runtime routing, and preserves effective Pages rewrite queries through handler metadata and split handoffs.

Changes: Middleware matcher compatibility

Layer / File(s) Summary
Locale pattern analysis and work budgeting
packages/core/src/request/middleware-locale.ts, packages/core/src/request/pattern-budget.ts, packages/core/src/bundle/pattern-budget.ts
Locale pattern parsing now caches supported forms. Locale-aware analysis reserves request-scoped pattern work.
Payload suffix matching
packages/core/src/paas/payload-matcher.ts
Payload splitting supports JSON-only and RSC-capable suffixes. After budget refusal, locale-aware analysis can return false for a proven locale mismatch.
Matcher tests and CI coverage
.github/workflows/ci.yaml, packages/adapter/test/*
Tests compare emitted Next.js matchers across configurations and payload grammars. CI runs the adapter and runtime tests against Next.js 16.2.0.

Locale request capture and redirects

Layer / File(s) Summary
Locale request protocol
packages/core/src/paas/locale-request.ts, packages/core/src/paas/protocol.ts, packages/core/src/paas/middleware.ts, packages/core/src/paas/index.ts, packages/core/test/locale-request.test.ts
The locale request shape captures language and cookie values, supports validated Base64URL encoding, and is exposed through a platform request header.
Edge capture and forwarding
packages/edge/src/handlers/locale-request.ts, packages/edge/src/handlers/{document-upstream,image-source,image-source-rewrite,middleware,passthrough}.ts, packages/edge/src/route.ts, packages/edge/src/strategies/{context,continuation-resume}.ts, packages/edge/test/{image-source-routing,locale-request}.test.ts
The edge captures locale inputs for eligible roots and forwards them through middleware continuations. Tests cover overrides, rewrites, and paths that do not forward the carrier.
Runtime locale negotiation and redirect
packages/runtime/src/{handle,handoff,incoming,locale-language,locale-redirect,middleware-invoke,outputs,resolve-request,routing,serve,source-input}.ts, packages/runtime/test/locale-redirect.test.ts, packages/runtime/NOTICE
The runtime uses original locale inputs, language preferences, cookies, and domain configuration to select root locale redirects before middleware or routing continues.

Pages rewrite query propagation

Layer / File(s) Summary
Effective query routing and request metadata
packages/runtime/src/{handle,outputs,routing,serve,source-input}.ts
Routing returns the effective Pages query separately from the public URL and adds it to handler request metadata. Source requests clear routing metadata.
Query handoff and validation
packages/runtime/src/{handoff,handle}.ts, packages/runtime/test/{handoff,pages-rewrite-query}.test.ts
Handoffs carry validated effective queries. Tests check duplicate UTF-8 values and rewrite parameters through direct routing and split handoffs.

Priority: Pending latest changes

Estimated code review effort: 4 (Complex) | ~60 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant EdgeRoute
  participant rootLocaleRequest
  participant runMiddlewareIn
  participant resumeRequest
  participant initialRequestOf
  participant preferredLocaleRedirect
  EdgeRoute->>rootLocaleRequest: Capture locale inputs for an eligible root
  EdgeRoute->>runMiddlewareIn: Pass locale request in edge context
  runMiddlewareIn->>resumeRequest: Return continuation with locale request
  resumeRequest->>initialRequestOf: Forward encoded locale request header
  initialRequestOf->>preferredLocaleRedirect: Provide decoded original locale inputs
Loading

Possibly related PRs

  • arkorlab/upwind#286: Updates the Next.js version used for matcher comparisons, which defines a matcher contract tested here.
  • arkorlab/upwind#350: Adds pattern budgeting and locale-pattern decomposition used by this change’s bounded matcher analysis.
  • arkorlab/upwind#383: Changes Pages handler URL construction, which is also extended here with separate effective query propagation.

Suggested reviewers: k-taro56

Merge Risk: Minimal · previous review e3392

No issue requiring a change before merge was established. The added CI lane selects the intended tests, and no material locale-routing failure was found.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 61.54% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly summarizes the main changes: Next.js locale matching and Pages Router request routing. It is concise, specific, and directly related to the pull request.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 61.54% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 52 functions across 36 files. (2 skipped: 2 unsupported.)

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-29T14:01:19.713043Z 26f0f20 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The changes since the previous review appear safe to merge.

Summary

The PR fixes locale-aware middleware matching, preserves Pages rewrite queries, and adds original-root locale negotiation.

Since the previous review:

  • Locale-aware middleware matchers stay within the request pattern budget.
  • Eligible root requests can redirect using the visitor’s original locale preferences.
  • Pages handlers receive rewrite queries while keeping the source URL.

Reviews (6) · Last reviewed commit: "fix(runtime): negotiate locales once bef..." · Reviewed by Greptile

Comment thread packages/runtime/src/routing.ts Outdated
Comment thread packages/runtime/src/locale-redirect.ts Outdated
@soleil-colza soleil-colza changed the title Fix locale-aware middleware matching within request budgets Fix Next.js locale matching and Pages Router request routing Oct 29, 2026
Comment thread packages/runtime/src/locale-redirect.ts

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant