[PR #504] [MERGED] fix: preserve query params on middleware rewrite in App Router #626

Closed
opened 2026-05-06 13:09:10 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/504
Author: @NathanDrake2406
Created: 3/12/2026
Status: Merged
Merged: 3/12/2026
Merged by: @james-elicx

Base: mainHead: fix/middleware-rewrite-query-params


📝 Commits (2)

  • b6646e9 fix: preserve query params on middleware rewrite in App Router
  • 4e5c987 test: update entry-templates snapshot

📊 Changes

4 files changed (+27 additions, -0 deletions)

View changed files

📝 packages/vinext/src/entries/app-rsc-entry.ts (+4 -0)
📝 tests/__snapshots__/entry-templates.test.ts.snap (+4 -0)
📝 tests/app-router.test.ts (+10 -0)
📝 tests/fixtures/app-basic/middleware.ts (+9 -0)

📄 Description

Summary

  • App Router middleware rewrites silently dropped query params from the rewrite URL — only pathname was extracted, search was discarded
  • All downstream consumers (searchParams page props, useSearchParams(), navigation context, ISR cache keys) saw the original request's query string instead of the rewrite destination's
  • Pages Router already handled this correctly in server/middleware.ts:515 (pathname + search)

Fix

One line in app-rsc-entry.ts: after extracting rewriteParsed.pathname, also set url.search = rewriteParsed.search. Since url is a mutable URL object referenced by ~30 downstream call sites, this propagates automatically with zero additional changes.

Test plan

  • Added middleware rewrite route (/middleware-rewrite-query) that rewrites to /search-query?searchParams=from-rewrite&extra=injected
  • New test asserts "from-rewrite" appears in the rendered HTML
  • All 17 existing middleware tests pass
  • Search-related tests pass
  • CI: full Vitest + Playwright suite

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/504 **Author:** [@NathanDrake2406](https://github.com/NathanDrake2406) **Created:** 3/12/2026 **Status:** ✅ Merged **Merged:** 3/12/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `fix/middleware-rewrite-query-params` --- ### 📝 Commits (2) - [`b6646e9`](https://github.com/cloudflare/vinext/commit/b6646e9bdeb51eb481aa2e123bc20197bbee33be) fix: preserve query params on middleware rewrite in App Router - [`4e5c987`](https://github.com/cloudflare/vinext/commit/4e5c98723d748a85b212150c114eb9dcd0e301e6) test: update entry-templates snapshot ### 📊 Changes **4 files changed** (+27 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/entries/app-rsc-entry.ts` (+4 -0) 📝 `tests/__snapshots__/entry-templates.test.ts.snap` (+4 -0) 📝 `tests/app-router.test.ts` (+10 -0) 📝 `tests/fixtures/app-basic/middleware.ts` (+9 -0) </details> ### 📄 Description ## Summary - App Router middleware rewrites silently dropped query params from the rewrite URL — only `pathname` was extracted, `search` was discarded - All downstream consumers (`searchParams` page props, `useSearchParams()`, navigation context, ISR cache keys) saw the original request's query string instead of the rewrite destination's - Pages Router already handled this correctly in `server/middleware.ts:515` (`pathname + search`) ## Fix One line in `app-rsc-entry.ts`: after extracting `rewriteParsed.pathname`, also set `url.search = rewriteParsed.search`. Since `url` is a mutable `URL` object referenced by ~30 downstream call sites, this propagates automatically with zero additional changes. ## Test plan - [x] Added middleware rewrite route (`/middleware-rewrite-query`) that rewrites to `/search-query?searchParams=from-rewrite&extra=injected` - [x] New test asserts `"from-rewrite"` appears in the rendered HTML - [x] All 17 existing middleware tests pass - [x] Search-related tests pass - [ ] CI: full Vitest + Playwright suite --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:09:10 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#626
No description provided.