From 2bd30cf0f7ad549c35e0c4c9c9584d3514808331 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sat, 18 Jun 2022 00:10:11 +0200 Subject: [PATCH] revert dynamic routes render change --- packages/next/server/base-server.ts | 95 ++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 21 deletions(-) diff --git a/packages/next/server/base-server.ts b/packages/next/server/base-server.ts index f4fad12d6932c2..d8f32613211b9c 100644 --- a/packages/next/server/base-server.ts +++ b/packages/next/server/base-server.ts @@ -1761,35 +1761,88 @@ export default abstract class Server { const bubbleNoFallback = !!query._nextBubbleNoFallback delete query._nextBubbleNoFallback - let result - let error let page = pathname - let params: Params | undefined = undefined + const getOriginalappPath = (appPath: string) => { + if (this.nextConfig.experimental.appDir) { + const originalappPath = this.appPathRoutes?.[appPath] - // 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)) { - try { - result = await this.renderPage(ctx, params, page, bubbleNoFallback) - if (result) return result + if (!originalappPath) { + return null + } - if (this.dynamicRoutes) { - for (const dynamicRoute of this.dynamicRoutes) { - params = dynamicRoute.match(page) || undefined - if (params) { - page = dynamicRoute.page - break + 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) + + 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 + + if (!isNoFallbackError || (isNoFallbackError && bubbleNoFallback)) { + throw err } } } - result = await this.renderPage(ctx, params, page, bubbleNoFallback) - if (result) return result - } catch (err) { - error = err } - } - if (error) { + if (this.dynamicRoutes) { + for (const dynamicRoute of this.dynamicRoutes) { + const params = dynamicRoute.match(pathname) + if (!params) { + continue + } + page = dynamicRoute.page + const appPath = getOriginalappPath(page) + + if (typeof appPath === 'string') { + page = appPath + } + + const dynamicRouteResult = await this.findPageComponents( + page, + query, + params + ) + 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 + } + } + } + } + } + } catch (error) { const err = getProperError(error) if (err instanceof NoFallbackError && bubbleNoFallback) { throw err