Unverified Commit 9184ddb1 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

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

Backports:

- #93294
parent 3279f016
...@@ -227,9 +227,14 @@ function getMiddlewareData<T extends FetchDataOutput>( ...@@ -227,9 +227,14 @@ function getMiddlewareData<T extends FetchDataOutput>(
const parsedSource = getNextPathnameInfo( const parsedSource = getNextPathnameInfo(
parseRelativeUrl(source).pathname, parseRelativeUrl(source).pathname,
{ {
nextConfig: process.env.__NEXT_HAS_REWRITES // Pass basePath (and trailingSlash) so the basePath prefix is
? undefined // stripped before the `_next/data/` check, but omit `i18n` so the
: nextConfig, // locale prefix is preserved here — the rewrite resolver below
// handles the locale-prefixed `as`.
nextConfig: {
basePath: nextConfig.basePath,
trailingSlash: nextConfig.trailingSlash,
},
parseData: true, parseData: true,
} }
) )
......
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('middleware-dynamic-basepath-matcher-rewrites', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('preserves router.query on client-side navigation to a catch-all page', async () => {
const browser = await next.browser('/docs')
await browser.elementById('catchall-link').click()
await retry(async () => {
expect(await browser.elementById('page-title').text()).toBe('CatchAll')
})
expect(await browser.elementById('query-path').text()).toBe('["first"]')
})
})
import { NextResponse } from 'next/server'
export default function middleware() {
return NextResponse.next()
}
export const config = {
matcher: '/:path*',
}
module.exports = {
basePath: '/docs',
async rewrites() {
// Any non-empty rewrites array sets __NEXT_HAS_REWRITES=true; this entry
// only exists to flip that build flag, it isn't expected to match.
return [{ source: '/never-matched-rewrite', destination: '/never' }]
},
}
import { useRouter } from 'next/router'
export default function CatchAll() {
const router = useRouter()
return (
<div>
<h1 id="page-title">CatchAll</h1>
<p id="query-path">{JSON.stringify(router.query.path)}</p>
</div>
)
}
export async function getServerSideProps() {
return { props: {} }
}
import Link from 'next/link'
export default function Home() {
return (
<Link href="/first" id="catchall-link">
Go to catchall
</Link>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment