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

refactor: merge duplicated render page logic #37897

Merged
merged 1 commit into from
Jun 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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