Skip to content

Commit

Permalink
refactor: merge duplicated render page logic (#37897)
Browse files Browse the repository at this point in the history
Simplify rendering page logic (render page and match dynamic routes) in `renderToResponse`
  • Loading branch information
huozhi committed Jun 21, 2022
1 parent 5b854f3 commit a1b8a96
Showing 1 changed file with 56 additions and 63 deletions.
119 changes: 56 additions & 63 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1754,49 +1754,65 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return path
}

private async renderToResponse(
ctx: RequestContext
): Promise<ResponsePayload | null> {
const { res, query, pathname } = ctx
let page = pathname
const bubbleNoFallback = !!query._nextBubbleNoFallback
delete query._nextBubbleNoFallback
protected async renderPageComponent(
ctx: RequestContext,
bubbleNoFallback: boolean
) {
// map the route to the actual bundle name
const getOriginalappPath = (appPath: string) => {
const getOriginalAppPath = (appPath: string) => {
if (this.nextConfig.experimental.appDir) {
const originalappPath = this.appPathRoutes?.[appPath]
const originalAppPath = this.appPathRoutes?.[appPath]

if (!originalappPath) {
if (!originalAppPath) {
return null
}

return originalappPath
return originalAppPath
}
return null
}

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(pathname)) {
const appPath = getOriginalappPath(pathname)
const { query, pathname } = ctx
const appPath = getOriginalAppPath(pathname)

if (typeof appPath === 'string') {
page = appPath
}
const result = await this.findPageComponents(page, query)
if (result) {
try {
return await this.renderToResponseWithComponents(ctx, result)
} catch (err) {
const isNoFallbackError = err instanceof NoFallbackError
let page = pathname
if (typeof appPath === 'string') {
page = appPath
}
const result = await this.findPageComponents(
page,
query,
ctx.renderOpts.params
)
if (result) {
try {
return await this.renderToResponseWithComponents(ctx, result)
} catch (err) {
const isNoFallbackError = err instanceof NoFallbackError

if (!isNoFallbackError || (isNoFallbackError && bubbleNoFallback)) {
throw err
}
}
if (!isNoFallbackError || (isNoFallbackError && bubbleNoFallback)) {
throw err
}
}
}
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) {
for (const dynamicRoute of this.dynamicRoutes) {
Expand All @@ -1805,41 +1821,18 @@ export default abstract class Server<ServerOptions extends Options = Options> {
continue
}
page = dynamicRoute.page
const appPath = getOriginalappPath(page)

if (typeof appPath === 'string') {
page = appPath
}

const dynamicRouteResult = await this.findPageComponents(
page,
query,
params
const result = await this.renderPageComponent(
{
...ctx,
pathname: page,
renderOpts: {
...ctx.renderOpts,
params,
},
},
bubbleNoFallback
)
if (dynamicRouteResult) {
try {
return await this.renderToResponseWithComponents(
{
...ctx,
pathname: page,
renderOpts: {
...ctx.renderOpts,
params,
},
},
dynamicRouteResult
)
} catch (err) {
const isNoFallbackError = err instanceof NoFallbackError

if (
!isNoFallbackError ||
(isNoFallbackError && bubbleNoFallback)
) {
throw err
}
}
}
if (result) return result
}
}
} catch (error) {
Expand Down

0 comments on commit a1b8a96

Please sign in to comment.