Unverified Commit a1b8a960 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

refactor: merge duplicated render page logic (#37897)

Simplify rendering page logic (render page and match dynamic routes) in `renderToResponse`
parent 5b854f31
...@@ -1754,37 +1754,36 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1754,37 +1754,36 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return path return path
} }
private async renderToResponse( protected async renderPageComponent(
ctx: RequestContext ctx: RequestContext,
): Promise<ResponsePayload | null> { bubbleNoFallback: boolean
const { res, query, pathname } = ctx ) {
let page = pathname
const bubbleNoFallback = !!query._nextBubbleNoFallback
delete query._nextBubbleNoFallback
// map the route to the actual bundle name // map the route to the actual bundle name
const getOriginalappPath = (appPath: string) => { const getOriginalAppPath = (appPath: string) => {
if (this.nextConfig.experimental.appDir) { if (this.nextConfig.experimental.appDir) {
const originalappPath = this.appPathRoutes?.[appPath] const originalAppPath = this.appPathRoutes?.[appPath]
if (!originalappPath) { if (!originalAppPath) {
return null return null
} }
return originalappPath return originalAppPath
} }
return null return null
} }
try { const { query, pathname } = ctx
// Ensure a request to the URL /accounts/[id] will be treated as a dynamic const appPath = getOriginalAppPath(pathname)
// route correctly and not loaded immediately without parsing params.
if (!isDynamicRoute(pathname)) {
const appPath = getOriginalappPath(pathname)
let page = pathname
if (typeof appPath === 'string') { if (typeof appPath === 'string') {
page = appPath page = appPath
} }
const result = await this.findPageComponents(page, query) const result = await this.findPageComponents(
page,
query,
ctx.renderOpts.params
)
if (result) { if (result) {
try { try {
return await this.renderToResponseWithComponents(ctx, result) return await this.renderToResponseWithComponents(ctx, result)
...@@ -1796,6 +1795,23 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1796,6 +1795,23 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} }
} }
} }
return null
}
private async renderToResponse(
ctx: RequestContext
): Promise<ResponsePayload | null> {
const { res, query, pathname } = ctx
let page = pathname
const bubbleNoFallback = !!query._nextBubbleNoFallback
delete query._nextBubbleNoFallback
try {
// Ensure a request to the URL /accounts/[id] will be treated as a dynamic
// route correctly and not loaded immediately without parsing params.
if (!isDynamicRoute(page)) {
const result = await this.renderPageComponent(ctx, bubbleNoFallback)
if (result) return result
} }
if (this.dynamicRoutes) { if (this.dynamicRoutes) {
...@@ -1805,20 +1821,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1805,20 +1821,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
continue continue
} }
page = dynamicRoute.page page = dynamicRoute.page
const appPath = getOriginalappPath(page) const result = await this.renderPageComponent(
if (typeof appPath === 'string') {
page = appPath
}
const dynamicRouteResult = await this.findPageComponents(
page,
query,
params
)
if (dynamicRouteResult) {
try {
return await this.renderToResponseWithComponents(
{ {
...ctx, ...ctx,
pathname: page, pathname: page,
...@@ -1827,19 +1830,9 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1827,19 +1830,9 @@ export default abstract class Server<ServerOptions extends Options = Options> {
params, params,
}, },
}, },
dynamicRouteResult bubbleNoFallback
) )
} catch (err) { if (result) return result
const isNoFallbackError = err instanceof NoFallbackError
if (
!isNoFallbackError ||
(isNoFallbackError && bubbleNoFallback)
) {
throw err
}
}
}
} }
} }
} catch (error) { } catch (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