@@ -700,7 +700,7 @@ export default class Server {
700700 )
701701
702702 const { query } = parsedDestination
703- delete parsedDestination . query
703+ delete ( parsedDestination as any ) . query
704704
705705 parsedDestination . search = stringifyQs ( query , undefined , undefined , {
706706 encodeURIComponent : ( str : string ) => str ,
@@ -745,7 +745,7 @@ export default class Server {
745745 // external rewrite, proxy it
746746 if ( parsedDestination . protocol ) {
747747 const { query } = parsedDestination
748- delete parsedDestination . query
748+ delete ( parsedDestination as any ) . query
749749 parsedDestination . search = stringifyQs (
750750 query ,
751751 undefined ,
@@ -1116,6 +1116,7 @@ export default class Server {
11161116 ...( components . getStaticProps
11171117 ? {
11181118 amp : query . amp ,
1119+ __next404 : query . __next404 ,
11191120 _nextDataReq : query . _nextDataReq ,
11201121 __nextLocale : query . __nextLocale ,
11211122 }
@@ -1241,12 +1242,27 @@ export default class Server {
12411242 query . amp ? '.amp' : ''
12421243 } `
12431244
1245+ // In development we use a __next404 query to allow signaling we should
1246+ // render the 404 page after attempting to fetch the _next/data for a
1247+ // fallback page since the fallback page will always be available after
1248+ // reload and we don't want to re-serve it and instead want to 404.
1249+ if ( this . renderOpts . dev && isSSG && query . __next404 ) {
1250+ delete query . __next404
1251+ throw new NoFallbackError ( )
1252+ }
1253+
12441254 // Complete the response with cached data if its present
12451255 const cachedData = ssgCacheKey
12461256 ? await this . incrementalCache . get ( ssgCacheKey )
12471257 : undefined
12481258
12491259 if ( cachedData ) {
1260+ if ( cachedData . isNotFound ) {
1261+ // we don't currently revalidate when notFound is returned
1262+ // so trigger rendering 404 here
1263+ throw new NoFallbackError ( )
1264+ }
1265+
12501266 const data = isDataReq
12511267 ? JSON . stringify ( cachedData . pageData )
12521268 : cachedData . html
@@ -1291,10 +1307,12 @@ export default class Server {
12911307 html : string | null
12921308 pageData : any
12931309 sprRevalidate : number | false
1310+ isNotFound ?: boolean
12941311 } > => {
12951312 let pageData : any
12961313 let html : string | null
12971314 let sprRevalidate : number | false
1315+ let isNotFound : boolean | undefined
12981316
12991317 let renderResult
13001318 // handle serverless
@@ -1314,6 +1332,7 @@ export default class Server {
13141332 html = renderResult . html
13151333 pageData = renderResult . renderOpts . pageData
13161334 sprRevalidate = renderResult . renderOpts . revalidate
1335+ isNotFound = renderResult . renderOpts . ssgNotFound
13171336 } else {
13181337 const origQuery = parseUrl ( req . url || '' , true ) . query
13191338 const resolvedUrl = formatUrl ( {
@@ -1355,9 +1374,10 @@ export default class Server {
13551374 // TODO: change this to a different passing mechanism
13561375 pageData = ( renderOpts as any ) . pageData
13571376 sprRevalidate = ( renderOpts as any ) . revalidate
1377+ isNotFound = ( renderOpts as any ) . ssgNotFound
13581378 }
13591379
1360- return { html, pageData, sprRevalidate }
1380+ return { html, pageData, sprRevalidate, isNotFound }
13611381 }
13621382 )
13631383
@@ -1439,10 +1459,15 @@ export default class Server {
14391459
14401460 const {
14411461 isOrigin,
1442- value : { html, pageData, sprRevalidate } ,
1462+ value : { html, pageData, sprRevalidate, isNotFound } ,
14431463 } = await doRender ( )
14441464 let resHtml = html
1445- if ( ! isResSent ( res ) && ( isSSG || isDataReq || isServerProps ) ) {
1465+
1466+ if (
1467+ ! isResSent ( res ) &&
1468+ ! isNotFound &&
1469+ ( isSSG || isDataReq || isServerProps )
1470+ ) {
14461471 sendPayload (
14471472 req ,
14481473 res ,
@@ -1467,11 +1492,14 @@ export default class Server {
14671492 if ( isOrigin && ssgCacheKey ) {
14681493 await this . incrementalCache . set (
14691494 ssgCacheKey ,
1470- { html : html ! , pageData } ,
1495+ { html : html ! , pageData, isNotFound } ,
14711496 sprRevalidate
14721497 )
14731498 }
14741499
1500+ if ( isNotFound ) {
1501+ throw new NoFallbackError ( )
1502+ }
14751503 return resHtml
14761504 }
14771505
0 commit comments