Skip to content

Commit ebee2ba

Browse files
authored
Ensure 404 with SSG is rendered correctly with notFound (#18205)
This ensures a custom `/404` page with `getStaticProps` works correctly when leveraging the new `unstable_notFound` support in `getStaticProps` Closes: #18196 x-ref: #17755
1 parent 299fba1 commit ebee2ba

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

packages/next/next-server/server/next-server.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,8 +1183,10 @@ export default class Server {
11831183
{ components, query }: FindComponentsResult,
11841184
opts: RenderOptsPartial
11851185
): Promise<string | null> {
1186+
const is404Page = pathname === '/404'
1187+
11861188
// we need to ensure the status code if /404 is visited directly
1187-
if (pathname === '/404') {
1189+
if (is404Page) {
11881190
res.statusCode = 404
11891191
}
11901192

@@ -1255,13 +1257,19 @@ export default class Server {
12551257
urlPathname = stripNextDataPath(urlPathname)
12561258
}
12571259

1258-
const ssgCacheKey =
1260+
let ssgCacheKey =
12591261
isPreviewMode || !isSSG
12601262
? undefined // Preview mode bypasses the cache
12611263
: `${locale ? `/${locale}` : ''}${resolvedUrlPathname}${
12621264
query.amp ? '.amp' : ''
12631265
}`
12641266

1267+
if (is404Page && isSSG) {
1268+
ssgCacheKey = `${locale ? `/${locale}` : ''}${pathname}${
1269+
query.amp ? '.amp' : ''
1270+
}`
1271+
}
1272+
12651273
// Complete the response with cached data if its present
12661274
const cachedData = ssgCacheKey
12671275
? await this.incrementalCache.get(ssgCacheKey)
@@ -1307,7 +1315,7 @@ export default class Server {
13071315

13081316
// If we're here, that means data is missing or it's stale.
13091317
const maybeCoalesceInvoke = ssgCacheKey
1310-
? (fn: any) => withCoalescedInvoke(fn).bind(null, ssgCacheKey, [])
1318+
? (fn: any) => withCoalescedInvoke(fn).bind(null, ssgCacheKey!, [])
13111319
: (fn: any) => async () => {
13121320
const value = await fn()
13131321
return { isOrigin: true, value }

packages/next/next-server/server/render.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,12 @@ export async function renderToHTML(
637637
}
638638

639639
if (data.unstable_notFound) {
640+
if (pathname === '/404') {
641+
throw new Error(
642+
`The /404 page can not return unstable_notFound in "getStaticProps", please remove it to continue!`
643+
)
644+
}
645+
640646
;(renderOpts as any).ssgNotFound = true
641647
;(renderOpts as any).revalidate = false
642648
return null
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default function NotFound(props) {
2+
return (
3+
<>
4+
<h1 id="not-found">This page could not be found | 404</h1>
5+
<p id="prop">{JSON.stringify(props)}</p>
6+
</>
7+
)
8+
}
9+
10+
export const getStaticProps = () => {
11+
return {
12+
props: {
13+
is404: true,
14+
},
15+
}
16+
}

test/integration/i18n-support/test/index.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ function runTests(isDev) {
7979
initialRevalidateSeconds: false,
8080
srcRoute: null,
8181
},
82+
'/404': {
83+
dataRoute: `/_next/data/${buildId}/404.json`,
84+
initialRevalidateSeconds: false,
85+
srcRoute: null,
86+
},
8287
'/en-US/gsp/fallback/first': {
8388
dataRoute: `/_next/data/${buildId}/en-US/gsp/fallback/first.json`,
8489
initialRevalidateSeconds: false,

yarn.lock

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15826,9 +15826,10 @@ styled-jsx-plugin-postcss@2.0.1:
1582615826
postcss "^7.0.2"
1582715827
postcss-load-plugins "^2.3.0"
1582815828

15829-
styled-jsx@3.3.0:
15830-
version "3.3.0"
15831-
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-3.3.0.tgz#32335c1a3ecfc923ba4f9c056eeb3d4699006b09"
15829+
styled-jsx@3.3.1:
15830+
version "3.3.1"
15831+
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-3.3.1.tgz#d79f306c42c99cefbe8e76f35dad8100dc5c9ecc"
15832+
integrity sha512-RhW71t3k95E3g7Zq3lEBk+kmf+p4ZME7c5tfsYf9M5mq6CgIvFXkbvhawL2gWriXLRlMyKAYACE89Qa2JnTqUw==
1583215833
dependencies:
1583315834
"@babel/types" "7.8.3"
1583415835
babel-plugin-syntax-jsx "6.18.0"

0 commit comments

Comments
 (0)