Repository navigation
Description
Environment
- nitro:
[email protected]— the bug is still present inmainas of 2026-07-07:src/presets/vercel/runtime/isr.ts - Deployment: Vercel (ISR / prerender functions)
- Framework: TanStack Start, but this is at the preset-runtime level and framework-agnostic
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:
- Route rule:
{ isr: { expiration: 86400, allowQuery: ["lang"], passQuery: true } }. - Make the page render differently based on
?lang. - Local
vite preview:/?lang=esrenders the Spanish variant — correct. - Deployed on Vercel:
/?lang=esrenders the bare-path (English) document, which the ISR cache then stores under the?lang=eskey — wrong.
Related
- Problem when using ISR passQuery on routeRules when deployed to Vercel #3594 — closed by the
passQueryfixes, but the query still never reaches the app whenx-now-route-matchesis present. - fix(vercel): handle ISR requests with
passQuery: true#3539 / fix(vercel): handle ISR requests withpassQuery: true#3851 — madepassQuery: trueforward the query to the function; the runtime rewrite then discards it in the header branch, so those PRs are effectively neutralized in the common prerender-function path.
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
fix(vercel): preserve query string in ISR rewrite when x-now-route-ma…