Unverified Commit 3ca022a8 authored by Gerald Monaco's avatar Gerald Monaco Committed by GitHub

Throw NoFallbackError instead of returning (#10793)

* Throw a NoFallbackError instead of returning

* Replace invariant
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 6598e99b
...@@ -871,7 +871,7 @@ export default class Server { ...@@ -871,7 +871,7 @@ export default class Server {
pathname: string, pathname: string,
{ components, query }: FindComponentsResult, { components, query }: FindComponentsResult,
opts: RenderOptsPartial opts: RenderOptsPartial
): Promise<string | false | null> { ): Promise<string | null> {
// 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 (pathname === '/404') {
res.statusCode = 404 res.statusCode = 404
...@@ -1076,7 +1076,7 @@ export default class Server { ...@@ -1076,7 +1076,7 @@ export default class Server {
// When fallback isn't present, abort this render so we 404 // When fallback isn't present, abort this render so we 404
!hasStaticFallback !hasStaticFallback
) { ) {
return false throw new NoFallbackError()
} }
let html: string let html: string
...@@ -1137,15 +1137,18 @@ export default class Server { ...@@ -1137,15 +1137,18 @@ export default class Server {
try { try {
const result = await this.findPageComponents(pathname, query) const result = await this.findPageComponents(pathname, query)
if (result) { if (result) {
const result2 = await this.renderToHTMLWithComponents( try {
return await this.renderToHTMLWithComponents(
req, req,
res, res,
pathname, pathname,
result, result,
{ ...this.renderOpts } { ...this.renderOpts }
) )
if (result2 !== false) { } catch (err) {
return result2 if (!(err instanceof NoFallbackError)) {
throw err
}
} }
} }
...@@ -1162,15 +1165,18 @@ export default class Server { ...@@ -1162,15 +1165,18 @@ export default class Server {
params params
) )
if (result) { if (result) {
const result2 = await this.renderToHTMLWithComponents( try {
return await this.renderToHTMLWithComponents(
req, req,
res, res,
dynamicRoute.page, dynamicRoute.page,
result, result,
{ ...this.renderOpts, params } { ...this.renderOpts, params }
) )
if (result2 !== false) { } catch (err) {
return result2 if (!(err instanceof NoFallbackError)) {
throw err
}
} }
} }
} }
...@@ -1227,7 +1233,8 @@ export default class Server { ...@@ -1227,7 +1233,8 @@ export default class Server {
let html: string | null let html: string | null
try { try {
const result2 = await this.renderToHTMLWithComponents( try {
html = await this.renderToHTMLWithComponents(
req, req,
res, res,
using404Page ? '/404' : '/_error', using404Page ? '/404' : '/_error',
...@@ -1237,10 +1244,12 @@ export default class Server { ...@@ -1237,10 +1244,12 @@ export default class Server {
err, err,
} }
) )
if (result2 === false) { } catch (err) {
if (err instanceof NoFallbackError) {
throw new Error('invariant: failed to render error page') throw new Error('invariant: failed to render error page')
} }
html = result2 throw err
}
} catch (err) { } catch (err) {
console.error(err) console.error(err)
res.statusCode = 500 res.statusCode = 500
...@@ -1367,3 +1376,5 @@ function prepareServerlessUrl(req: IncomingMessage, query: ParsedUrlQuery) { ...@@ -1367,3 +1376,5 @@ function prepareServerlessUrl(req: IncomingMessage, query: ParsedUrlQuery) {
}, },
}) })
} }
class NoFallbackError extends Error {}
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