Unverified Commit ebee2baf authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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: https://github.com/vercel/next.js/issues/18196
x-ref: https://github.com/vercel/next.js/pull/17755
parent 299fba16
...@@ -1183,8 +1183,10 @@ export default class Server { ...@@ -1183,8 +1183,10 @@ export default class Server {
{ components, query }: FindComponentsResult, { components, query }: FindComponentsResult,
opts: RenderOptsPartial opts: RenderOptsPartial
): Promise<string | null> { ): Promise<string | null> {
const is404Page = pathname === '/404'
// we need to ensure the status code if /404 is visited directly // we need to ensure the status code if /404 is visited directly
if (pathname === '/404') { if (is404Page) {
res.statusCode = 404 res.statusCode = 404
} }
...@@ -1255,13 +1257,19 @@ export default class Server { ...@@ -1255,13 +1257,19 @@ export default class Server {
urlPathname = stripNextDataPath(urlPathname) urlPathname = stripNextDataPath(urlPathname)
} }
const ssgCacheKey = let ssgCacheKey =
isPreviewMode || !isSSG isPreviewMode || !isSSG
? undefined // Preview mode bypasses the cache ? undefined // Preview mode bypasses the cache
: `${locale ? `/${locale}` : ''}${resolvedUrlPathname}${ : `${locale ? `/${locale}` : ''}${resolvedUrlPathname}${
query.amp ? '.amp' : '' query.amp ? '.amp' : ''
}` }`
if (is404Page && isSSG) {
ssgCacheKey = `${locale ? `/${locale}` : ''}${pathname}${
query.amp ? '.amp' : ''
}`
}
// Complete the response with cached data if its present // Complete the response with cached data if its present
const cachedData = ssgCacheKey const cachedData = ssgCacheKey
? await this.incrementalCache.get(ssgCacheKey) ? await this.incrementalCache.get(ssgCacheKey)
...@@ -1307,7 +1315,7 @@ export default class Server { ...@@ -1307,7 +1315,7 @@ export default class Server {
// If we're here, that means data is missing or it's stale. // If we're here, that means data is missing or it's stale.
const maybeCoalesceInvoke = ssgCacheKey const maybeCoalesceInvoke = ssgCacheKey
? (fn: any) => withCoalescedInvoke(fn).bind(null, ssgCacheKey, []) ? (fn: any) => withCoalescedInvoke(fn).bind(null, ssgCacheKey!, [])
: (fn: any) => async () => { : (fn: any) => async () => {
const value = await fn() const value = await fn()
return { isOrigin: true, value } return { isOrigin: true, value }
......
...@@ -637,6 +637,12 @@ export async function renderToHTML( ...@@ -637,6 +637,12 @@ export async function renderToHTML(
} }
if (data.unstable_notFound) { if (data.unstable_notFound) {
if (pathname === '/404') {
throw new Error(
`The /404 page can not return unstable_notFound in "getStaticProps", please remove it to continue!`
)
}
;(renderOpts as any).ssgNotFound = true ;(renderOpts as any).ssgNotFound = true
;(renderOpts as any).revalidate = false ;(renderOpts as any).revalidate = false
return null return null
......
export default function NotFound(props) {
return (
<>
<h1 id="not-found">This page could not be found | 404</h1>
<p id="prop">{JSON.stringify(props)}</p>
</>
)
}
export const getStaticProps = () => {
return {
props: {
is404: true,
},
}
}
...@@ -79,6 +79,11 @@ function runTests(isDev) { ...@@ -79,6 +79,11 @@ function runTests(isDev) {
initialRevalidateSeconds: false, initialRevalidateSeconds: false,
srcRoute: null, srcRoute: null,
}, },
'/404': {
dataRoute: `/_next/data/${buildId}/404.json`,
initialRevalidateSeconds: false,
srcRoute: null,
},
'/en-US/gsp/fallback/first': { '/en-US/gsp/fallback/first': {
dataRoute: `/_next/data/${buildId}/en-US/gsp/fallback/first.json`, dataRoute: `/_next/data/${buildId}/en-US/gsp/fallback/first.json`,
initialRevalidateSeconds: false, initialRevalidateSeconds: false,
......
...@@ -15826,9 +15826,10 @@ styled-jsx-plugin-postcss@2.0.1: ...@@ -15826,9 +15826,10 @@ styled-jsx-plugin-postcss@2.0.1:
postcss "^7.0.2" postcss "^7.0.2"
postcss-load-plugins "^2.3.0" postcss-load-plugins "^2.3.0"
styled-jsx@3.3.0: styled-jsx@3.3.1:
version "3.3.0" version "3.3.1"
resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-3.3.0.tgz#32335c1a3ecfc923ba4f9c056eeb3d4699006b09" resolved "https://registry.yarnpkg.com/styled-jsx/-/styled-jsx-3.3.1.tgz#d79f306c42c99cefbe8e76f35dad8100dc5c9ecc"
integrity sha512-RhW71t3k95E3g7Zq3lEBk+kmf+p4ZME7c5tfsYf9M5mq6CgIvFXkbvhawL2gWriXLRlMyKAYACE89Qa2JnTqUw==
dependencies: dependencies:
"@babel/types" "7.8.3" "@babel/types" "7.8.3"
babel-plugin-syntax-jsx "6.18.0" babel-plugin-syntax-jsx "6.18.0"
......
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