Skip to content

Commit f1629d9

Browse files
devjiwonchoiijjk
andauthored
Backport "[Pages] fix: _error page's req.url can be overwritten t… (#82377)
…o dynamic param on minimal mode (#82347)" Co-authored-by: JJ Kasper <[email protected]>
1 parent b9aab5d commit f1629d9

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

packages/next/src/server/base-server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2649,7 +2649,13 @@ export default abstract class Server<
26492649
initPathname = normalizer.normalize(initPathname)
26502650
}
26512651
}
2652-
request.url = `${initPathname}${parsedInitUrl.search || ''}`
2652+
2653+
// On minimal mode, the request url of dynamic route can be a
2654+
// literal dynamic route ('/[slug]') instead of actual URL, so overwriting to initPathname
2655+
// will transform back the resolved url to the dynamic route pathname.
2656+
if (!(this.minimalMode && isErrorPathname)) {
2657+
request.url = `${initPathname}${parsedInitUrl.search || ''}`
2658+
}
26532659

26542660
// propagate the request context for dev
26552661
setRequestMeta(request, getRequestMeta(req))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('error-handler-not-found-req-url', () => {
4+
const { next } = nextTestSetup({
5+
files: __dirname,
6+
})
7+
8+
it('should log the correct request url and asPath for not found _error page', async () => {
9+
const browser = await next.browser('/3')
10+
const p = await browser.elementByCss('p')
11+
expect(await p.text()).toBe('reqUrl: /3, asPath: /3')
12+
})
13+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default function Page() {
2+
return <p>hello world</p>
3+
}
4+
5+
export async function getStaticProps() {
6+
return {
7+
notFound: true,
8+
}
9+
}
10+
11+
export async function getStaticPaths() {
12+
return {
13+
paths: [],
14+
fallback: 'blocking',
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { NextPageContext } from 'next'
2+
3+
Error.getInitialProps = (ctx: NextPageContext) => {
4+
return {
5+
reqUrl: ctx.req?.url,
6+
asPath: ctx.asPath,
7+
}
8+
}
9+
10+
export default function Error({
11+
reqUrl,
12+
asPath,
13+
}: {
14+
reqUrl?: string
15+
asPath?: string
16+
}) {
17+
return (
18+
<p>
19+
reqUrl: {reqUrl}, asPath: {asPath}
20+
</p>
21+
)
22+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <p>hello world</p>
3+
}

0 commit comments

Comments
 (0)