Skip to content

Commit

Permalink
Throw NoFallbackError instead of returning (#10793)
Browse files Browse the repository at this point in the history
* Throw a NoFallbackError instead of returning

* Replace invariant

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
devknoll and ijjk committed Mar 3, 2020
1 parent 6598e99 commit 3ca022a
Showing 1 changed file with 43 additions and 32 deletions.
75 changes: 43 additions & 32 deletions packages/next/next-server/server/next-server.ts
Expand Up @@ -871,7 +871,7 @@ export default class Server {
pathname: string,
{ components, query }: FindComponentsResult,
opts: RenderOptsPartial
): Promise<string | false | null> {
): Promise<string | null> {
// we need to ensure the status code if /404 is visited directly
if (pathname === '/404') {
res.statusCode = 404
Expand Down Expand Up @@ -1076,7 +1076,7 @@ export default class Server {
// When fallback isn't present, abort this render so we 404
!hasStaticFallback
) {
return false
throw new NoFallbackError()
}

let html: string
Expand Down Expand Up @@ -1137,15 +1137,18 @@ export default class Server {
try {
const result = await this.findPageComponents(pathname, query)
if (result) {
const result2 = await this.renderToHTMLWithComponents(
req,
res,
pathname,
result,
{ ...this.renderOpts }
)
if (result2 !== false) {
return result2
try {
return await this.renderToHTMLWithComponents(
req,
res,
pathname,
result,
{ ...this.renderOpts }
)
} catch (err) {
if (!(err instanceof NoFallbackError)) {
throw err
}
}
}

Expand All @@ -1162,15 +1165,18 @@ export default class Server {
params
)
if (result) {
const result2 = await this.renderToHTMLWithComponents(
req,
res,
dynamicRoute.page,
result,
{ ...this.renderOpts, params }
)
if (result2 !== false) {
return result2
try {
return await this.renderToHTMLWithComponents(
req,
res,
dynamicRoute.page,
result,
{ ...this.renderOpts, params }
)
} catch (err) {
if (!(err instanceof NoFallbackError)) {
throw err
}
}
}
}
Expand Down Expand Up @@ -1227,20 +1233,23 @@ export default class Server {

let html: string | null
try {
const result2 = await this.renderToHTMLWithComponents(
req,
res,
using404Page ? '/404' : '/_error',
result!,
{
...this.renderOpts,
err,
try {
html = await this.renderToHTMLWithComponents(
req,
res,
using404Page ? '/404' : '/_error',
result!,
{
...this.renderOpts,
err,
}
)
} catch (err) {
if (err instanceof NoFallbackError) {
throw new Error('invariant: failed to render error page')
}
)
if (result2 === false) {
throw new Error('invariant: failed to render error page')
throw err
}
html = result2
} catch (err) {
console.error(err)
res.statusCode = 500
Expand Down Expand Up @@ -1367,3 +1376,5 @@ function prepareServerlessUrl(req: IncomingMessage, query: ParsedUrlQuery) {
},
})
}

class NoFallbackError extends Error {}

0 comments on commit 3ca022a

Please sign in to comment.