Skip to content

[backport] Fix catch-all router.query corruption with basePath + rewrites#93917

Merged
unstubbable merged 1 commit into
next-16-2from
hl/backport-93294
May 19, 2026
Merged

[backport] Fix catch-all router.query corruption with basePath + rewrites#93917
unstubbable merged 1 commit into
next-16-2from
hl/backport-93294

Conversation

@unstubbable

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented May 18, 2026

Copy link
Copy Markdown
Contributor

Tests Passed

Commit: 7f13588

…93294)

In a Pages Router app that combines a `basePath`, `next.config.js`
`rewrites`, middleware, and a catch-all dynamic route, a client-side
`Link` navigation could land on the page with a corrupted
`router.query`. Instead of holding the captured route segments, the
catch-all param ended up holding the segments of the internal
`_next/data/<buildId>/...` URL. Page code that builds further URLs from
`router.query` then produced malformed paths and 404s in production.

The corruption originated in `getMiddlewareData`, which was passing
`nextConfig: undefined` to `getNextPathnameInfo` whenever
`__NEXT_HAS_REWRITES` was true. With basePath set, that meant the
basePath prefix was never stripped from the data-source URL, so the
`/_next/data/` check never matched and the data prefix was left intact.
The resulting pathname flowed back into `routeInfo.resolvedAs`, and on
catch-all routes the route regex matched it and overwrote
`router.query.<param>` with the data-URL segments.

The original ternary at this call site (added in #48753) was there to
avoid stripping the locale prefix, since `resolveRewrites` needs `as` to
keep its locale prefix to match locale-aware rewrite sources. Disabling
all of `nextConfig` was too coarse and broke basePath stripping in the
process. The fix passes a partial config with only `basePath` and
`trailingSlash`, which keeps the locale prefix intact while still
letting `getNextPathnameInfo` strip basePath before the data-prefix
check.

The new `test/e2e/middleware-dynamic-basepath-matcher-rewrites/` fixture
is the catch-all + basePath + rewrites + middleware variant of
`test/e2e/middleware-dynamic-basepath-matcher/` (#48753's regression
test), and asserts that `router.query.path` is preserved across a
client-side `Link` navigation.
daltino

This comment was marked as spam.

@unstubbable unstubbable enabled auto-merge (squash) May 18, 2026 17:44
@unstubbable unstubbable merged commit 9184ddb into next-16-2 May 19, 2026
139 of 142 checks passed
@unstubbable unstubbable deleted the hl/backport-93294 branch May 19, 2026 07:08
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.

3 participants