Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw NoFallbackError instead of returning #10793

Merged
merged 5 commits into from Mar 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
devknoll marked this conversation as resolved.
Show resolved Hide resolved
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 {}