@@ -699,7 +699,7 @@ export default class Server {
699699 )
700700
701701 const { query } = parsedDestination
702- delete parsedDestination . query
702+ delete ( parsedDestination as any ) . query
703703
704704 parsedDestination . search = stringifyQs ( query , undefined , undefined , {
705705 encodeURIComponent : ( str : string ) => str ,
@@ -744,7 +744,7 @@ export default class Server {
744744 // external rewrite, proxy it
745745 if ( parsedDestination . protocol ) {
746746 const { query } = parsedDestination
747- delete parsedDestination . query
747+ delete ( parsedDestination as any ) . query
748748 parsedDestination . search = stringifyQs (
749749 query ,
750750 undefined ,
@@ -1115,6 +1115,7 @@ export default class Server {
11151115 ...( components . getStaticProps
11161116 ? {
11171117 amp : query . amp ,
1118+ __next404 : query . __next404 ,
11181119 _nextDataReq : query . _nextDataReq ,
11191120 __nextLocale : query . __nextLocale ,
11201121 }
@@ -1240,12 +1241,27 @@ export default class Server {
12401241 query . amp ? '.amp' : ''
12411242 } `
12421243
1244+ // In development we use a __next404 query to allow signaling we should
1245+ // render the 404 page after attempting to fetch the _next/data for a
1246+ // fallback page since the fallback page will always be available after
1247+ // reload and we don't want to re-serve it and instead want to 404.
1248+ if ( this . renderOpts . dev && isSSG && query . __next404 ) {
1249+ delete query . __next404
1250+ throw new NoFallbackError ( )
1251+ }
1252+
12431253 // Complete the response with cached data if its present
12441254 const cachedData = ssgCacheKey
12451255 ? await this . incrementalCache . get ( ssgCacheKey )
12461256 : undefined
12471257
12481258 if ( cachedData ) {
1259+ if ( cachedData . isNotFound ) {
1260+ // we don't currently revalidate when notFound is returned
1261+ // so trigger rendering 404 here
1262+ throw new NoFallbackError ( )
1263+ }
1264+
12491265 const data = isDataReq
12501266 ? JSON . stringify ( cachedData . pageData )
12511267 : cachedData . html
@@ -1290,10 +1306,12 @@ export default class Server {
12901306 html : string | null
12911307 pageData : any
12921308 sprRevalidate : number | false
1309+ isNotFound ?: boolean
12931310 } > => {
12941311 let pageData : any
12951312 let html : string | null
12961313 let sprRevalidate : number | false
1314+ let isNotFound : boolean | undefined
12971315
12981316 let renderResult
12991317 // handle serverless
@@ -1313,6 +1331,7 @@ export default class Server {
13131331 html = renderResult . html
13141332 pageData = renderResult . renderOpts . pageData
13151333 sprRevalidate = renderResult . renderOpts . revalidate
1334+ isNotFound = renderResult . renderOpts . ssgNotFound
13161335 } else {
13171336 const origQuery = parseUrl ( req . url || '' , true ) . query
13181337 const resolvedUrl = formatUrl ( {
@@ -1354,9 +1373,10 @@ export default class Server {
13541373 // TODO: change this to a different passing mechanism
13551374 pageData = ( renderOpts as any ) . pageData
13561375 sprRevalidate = ( renderOpts as any ) . revalidate
1376+ isNotFound = ( renderOpts as any ) . ssgNotFound
13571377 }
13581378
1359- return { html, pageData, sprRevalidate }
1379+ return { html, pageData, sprRevalidate, isNotFound }
13601380 }
13611381 )
13621382
@@ -1438,10 +1458,15 @@ export default class Server {
14381458
14391459 const {
14401460 isOrigin,
1441- value : { html, pageData, sprRevalidate } ,
1461+ value : { html, pageData, sprRevalidate, isNotFound } ,
14421462 } = await doRender ( )
14431463 let resHtml = html
1444- if ( ! isResSent ( res ) && ( isSSG || isDataReq || isServerProps ) ) {
1464+
1465+ if (
1466+ ! isResSent ( res ) &&
1467+ ! isNotFound &&
1468+ ( isSSG || isDataReq || isServerProps )
1469+ ) {
14451470 sendPayload (
14461471 req ,
14471472 res ,
@@ -1466,11 +1491,14 @@ export default class Server {
14661491 if ( isOrigin && ssgCacheKey ) {
14671492 await this . incrementalCache . set (
14681493 ssgCacheKey ,
1469- { html : html ! , pageData } ,
1494+ { html : html ! , pageData, isNotFound } ,
14701495 sprRevalidate
14711496 )
14721497 }
14731498
1499+ if ( isNotFound ) {
1500+ throw new NoFallbackError ( )
1501+ }
14741502 return resHtml
14751503 }
14761504
0 commit comments