未验证 提交 3ca022a8 编写于 作者: G Gerald Monaco 提交者: GitHub

Throw NoFallbackError instead of returning (#10793)

* Throw a NoFallbackError instead of returning

* Replace invariant
Co-authored-by: NJJ Kasper <jj@jjsweb.site>
上级 6598e99b
......@@ -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
......@@ -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
......@@ -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
}
}
}
......@@ -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
}
}
}
}
......@@ -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
......@@ -1367,3 +1376,5 @@ function prepareServerlessUrl(req: IncomingMessage, query: ParsedUrlQuery) {
},
})
}
class NoFallbackError extends Error {}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册