Unverified Commit f1629d93 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

Backport "[Pages] fix: `_error` page's `req.url` can be overwritten t… (#82377)

…o dynamic param on minimal mode (#82347)"
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent b9aab5db
...@@ -2649,7 +2649,13 @@ export default abstract class Server< ...@@ -2649,7 +2649,13 @@ export default abstract class Server<
initPathname = normalizer.normalize(initPathname) initPathname = normalizer.normalize(initPathname)
} }
} }
request.url = `${initPathname}${parsedInitUrl.search || ''}`
// On minimal mode, the request url of dynamic route can be a
// literal dynamic route ('/[slug]') instead of actual URL, so overwriting to initPathname
// will transform back the resolved url to the dynamic route pathname.
if (!(this.minimalMode && isErrorPathname)) {
request.url = `${initPathname}${parsedInitUrl.search || ''}`
}
// propagate the request context for dev // propagate the request context for dev
setRequestMeta(request, getRequestMeta(req)) setRequestMeta(request, getRequestMeta(req))
......
import { nextTestSetup } from 'e2e-utils'
describe('error-handler-not-found-req-url', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should log the correct request url and asPath for not found _error page', async () => {
const browser = await next.browser('/3')
const p = await browser.elementByCss('p')
expect(await p.text()).toBe('reqUrl: /3, asPath: /3')
})
})
export default function Page() {
return <p>hello world</p>
}
export async function getStaticProps() {
return {
notFound: true,
}
}
export async function getStaticPaths() {
return {
paths: [],
fallback: 'blocking',
}
}
import type { NextPageContext } from 'next'
Error.getInitialProps = (ctx: NextPageContext) => {
return {
reqUrl: ctx.req?.url,
asPath: ctx.asPath,
}
}
export default function Error({
reqUrl,
asPath,
}: {
reqUrl?: string
asPath?: string
}) {
return (
<p>
reqUrl: {reqUrl}, asPath: {asPath}
</p>
)
}
export default function Page() {
return <p>hello world</p>
}
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