Skip to content

Vercel ISR: isrRouteRewrite drops the entire query string when x-now-route-matches is present (allowQuery/passQuery params never reach the render) #4408

Description

@jm-fuster

Environment

Describe the bug

isrRouteRewrite handles its two branches asymmetrically:

export function isrRouteRewrite(
  reqUrl: string,
  xNowRouteMatches: string | null
): [pathname: string, search: string] | undefined {
  if (xNowRouteMatches) {
    const isrURL = new URLSearchParams(xNowRouteMatches).get(ISR_URL_PARAM);
    if (isrURL) {
      return [decodeURIComponent(isrURL), ""]; // ← the ENTIRE query string is discarded
    }
  } else {
    const queryIndex = reqUrl.indexOf("?");
    if (queryIndex !== -1) {
      const params = new URLSearchParams(reqUrl.slice(queryIndex + 1));
      const isrURL = params.get(ISR_URL_PARAM);
      if (isrURL) {
        params.delete(ISR_URL_PARAM);
        return [decodeURIComponent(isrURL), params.toString()]; // ← query preserved (only __isr_route removed)
      }
    }
  }
}

When the invocation arrives with the x-now-route-matches header — the normal case for Vercel prerender functions — the rewritten URL comes back with an empty query string. The header-less branch correctly deletes only __isr_route and keeps everything else.

With an ISR route rule like:

routeRules: {
  "/": { isr: { expiration: 86400, allowQuery: ["lang"], passQuery: true } },
}

Vercel does its part correctly: it keys the ISR cache by ?lang (allowQuery) and, thanks to passQuery: true (#3539 / #3851), forwards the query to the function. But the runtime then throws the query away while rebuilding the URL, so SSR always renders the bare path — and the ISR cache stores that document under the query-keyed cache entry.

Real-world impact for us: English HTML was served (and cached, and indexed) at every ?lang=es URL, de-indexing the Spanish variant of the site. Everything renders correctly locally (vite preview); only Vercel deployments are affected, which makes this easy to miss.

Reproduction

Any ISR page on Vercel whose head/body depends on an allowed query param:

  1. Route rule: { isr: { expiration: 86400, allowQuery: ["lang"], passQuery: true } }.
  2. Make the page render differently based on ?lang.
  3. Local vite preview: /?lang=es renders the Spanish variant — correct.
  4. Deployed on Vercel: /?lang=es renders the bare-path (English) document, which the ISR cache then stores under the ?lang=es key — wrong.

Related

Proposed fix (running in production)

In the x-now-route-matches branch, rebuild the query string by merging the params from x-now-route-matches with the params of the rewritten request URL, skipping __isr_route and purely-numeric keys (Vercel's regex capture groups, which appear in x-now-route-matches as 0, 1, …).

This has been running in production at https://jorgemolinafuster.com since 2026-07-07, applied via pnpm patch against the published dist/ of the nightly:

diff --git a/dist/presets/vercel/runtime/isr.mjs b/dist/presets/vercel/runtime/isr.mjs
index 07d78beb143e771a249668beeb9eda1873f7beea..76f5321c21665f1fe6e8900318f5cda8ce9c5d65 100644
--- a/dist/presets/vercel/runtime/isr.mjs
+++ b/dist/presets/vercel/runtime/isr.mjs
@@ -1,19 +1,34 @@
 export const ISR_URL_PARAM = "__isr_route";
 export function isrRouteRewrite(reqUrl, xNowRouteMatches) {
+	const queryIndex = reqUrl.indexOf("?");
+	const reqParams = new URLSearchParams(
+		queryIndex === -1 ? "" : reqUrl.slice(queryIndex + 1)
+	);
 	if (xNowRouteMatches) {
-		const isrURL = new URLSearchParams(xNowRouteMatches).get(ISR_URL_PARAM);
+		const matches = new URLSearchParams(xNowRouteMatches);
+		const isrURL = matches.get(ISR_URL_PARAM);
 		if (isrURL) {
-			return [decodeURIComponent(isrURL), ""];
-		}
-	} else {
-		const queryIndex = reqUrl.indexOf("?");
-		if (queryIndex !== -1) {
-			const params = new URLSearchParams(reqUrl.slice(queryIndex + 1));
-			const isrURL = params.get(ISR_URL_PARAM);
-			if (isrURL) {
-				params.delete(ISR_URL_PARAM);
-				return [decodeURIComponent(isrURL), params.toString()];
+			// PATCHED (portfolio): the upstream version returns an empty query
+			// here, which drops every `allowQuery` param (e.g. ?lang) — the
+			// function then renders the bare path and the ISR cache stores that
+			// document under the query-keyed entry (EN HTML served at ?lang=es).
+			// Rebuild the query from both x-now-route-matches and the rewritten
+			// request URL, skipping the ISR routing param and Vercel's numeric
+			// regex-capture groups.
+			const params = new URLSearchParams();
+			for (const source of [matches, reqParams]) {
+				for (const [key, value] of source) {
+					if (key === ISR_URL_PARAM || /^\d+$/.test(key)) continue;
+					if (!params.has(key)) params.set(key, value);
+				}
 			}
+			return [decodeURIComponent(isrURL), params.toString()];
+		}
+	} else if (queryIndex !== -1) {
+		const isrURL = reqParams.get(ISR_URL_PARAM);
+		if (isrURL) {
+			reqParams.delete(ISR_URL_PARAM);
+			return [decodeURIComponent(isrURL), reqParams.toString()];
 		}
 	}
 }

Happy to open a PR porting this to src/presets/vercel/runtime/isr.ts (plus tests) if the approach looks right.

Activity

  1. added a commit that references this issue on Jul 7, 2026
    08e44a2
  2. added
    bug
    tanstack
    on Sep 2, 2026
  3. added theissue type on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtanstack

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions