Skip to content

Commit 9184ddb

Browse files
authored
[backport] Fix catch-all router.query corruption with basePath + rewrites (#93917)
Backports: - #93294
1 parent 3279f01 commit 9184ddb

6 files changed

Lines changed: 68 additions & 3 deletions

File tree

‎packages/next/src/shared/lib/router/router.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,14 @@ function getMiddlewareData<T extends FetchDataOutput>(
227227
const parsedSource = getNextPathnameInfo(
228228
parseRelativeUrl(source).pathname,
229229
{
230-
nextConfig: process.env.__NEXT_HAS_REWRITES
231-
? undefined
232-
: nextConfig,
230+
// Pass basePath (and trailingSlash) so the basePath prefix is
231+
// stripped before the `_next/data/` check, but omit `i18n` so the
232+
// locale prefix is preserved here — the rewrite resolver below
233+
// handles the locale-prefixed `as`.
234+
nextConfig: {
235+
basePath: nextConfig.basePath,
236+
trailingSlash: nextConfig.trailingSlash,
237+
},
233238
parseData: true,
234239
}
235240
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
import { retry } from 'next-test-utils'
3+
4+
describe('middleware-dynamic-basepath-matcher-rewrites', () => {
5+
const { next } = nextTestSetup({
6+
files: __dirname,
7+
})
8+
9+
it('preserves router.query on client-side navigation to a catch-all page', async () => {
10+
const browser = await next.browser('/docs')
11+
await browser.elementById('catchall-link').click()
12+
13+
await retry(async () => {
14+
expect(await browser.elementById('page-title').text()).toBe('CatchAll')
15+
})
16+
17+
expect(await browser.elementById('query-path').text()).toBe('["first"]')
18+
})
19+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse } from 'next/server'
2+
3+
export default function middleware() {
4+
return NextResponse.next()
5+
}
6+
7+
export const config = {
8+
matcher: '/:path*',
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
basePath: '/docs',
3+
async rewrites() {
4+
// Any non-empty rewrites array sets __NEXT_HAS_REWRITES=true; this entry
5+
// only exists to flip that build flag, it isn't expected to match.
6+
return [{ source: '/never-matched-rewrite', destination: '/never' }]
7+
},
8+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useRouter } from 'next/router'
2+
3+
export default function CatchAll() {
4+
const router = useRouter()
5+
return (
6+
<div>
7+
<h1 id="page-title">CatchAll</h1>
8+
<p id="query-path">{JSON.stringify(router.query.path)}</p>
9+
</div>
10+
)
11+
}
12+
13+
export async function getServerSideProps() {
14+
return { props: {} }
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Link from 'next/link'
2+
3+
export default function Home() {
4+
return (
5+
<Link href="/first" id="catchall-link">
6+
Go to catchall
7+
</Link>
8+
)
9+
}

0 commit comments

Comments
 (0)