diff --git a/packages/next/server/base-server.ts b/packages/next/server/base-server.ts index 23f636b5707d8..f4fad12d6932c 100644 --- a/packages/next/server/base-server.ts +++ b/packages/next/server/base-server.ts @@ -1762,51 +1762,34 @@ export default abstract class Server { delete query._nextBubbleNoFallback let result + let error let page = pathname let params: Params | undefined = undefined - // map the route to the actual bundle name - const getOriginalAppPath = (appPath: string) => { - if (this.nextConfig.experimental.appDir) { - const originalAppPath = this.appPathRoutes?.[appPath] - - if (!originalAppPath) { - return null - } - - return originalAppPath - } - return null - } - + // 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 appPath = getOriginalAppPath(page) - if (typeof appPath === 'string') { - page = appPath - } - result = await this.renderPage(ctx, params, page, bubbleNoFallback) - if (result) return result - } + try { + result = await this.renderPage(ctx, params, page, bubbleNoFallback) + if (result) return result - if (this.dynamicRoutes) { - for (const dynamicRoute of this.dynamicRoutes) { - params = dynamicRoute.match(page) || undefined - if (params) { - page = dynamicRoute.page - break + if (this.dynamicRoutes) { + for (const dynamicRoute of this.dynamicRoutes) { + params = dynamicRoute.match(page) || undefined + if (params) { + page = dynamicRoute.page + break + } + } } + result = await this.renderPage(ctx, params, page, bubbleNoFallback) + if (result) return result + } catch (err) { + error = err } } - const appPath = getOriginalAppPath(page) - if (typeof appPath === 'string') { - page = appPath - } - - try { - result = await this.renderPage(ctx, params, page, bubbleNoFallback) - if (result) return result - } catch (error) { + if (error) { const err = getProperError(error) if (err instanceof NoFallbackError && bubbleNoFallback) { throw err @@ -1862,7 +1845,26 @@ export default abstract class Server { page: string, bubbleNoFallback: boolean ) { + // map the route to the actual bundle name + const getOriginalAppPath = (appPath: string) => { + if (this.nextConfig.experimental.appDir) { + const originalAppPath = this.appPathRoutes?.[appPath] + + if (!originalAppPath) { + return null + } + + return originalAppPath + } + return null + } + const { query } = ctx + const appPath = getOriginalAppPath(page) + if (typeof appPath === 'string') { + page = appPath + } + const result = await this.findPageComponents(page, query, params) if (result) { try { diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index a66af00e750d3..628198504563c 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -1515,8 +1515,8 @@ export default class NextNodeServer extends BaseServer { } protected async runEdgeFunction(params: { - req: BaseNextRequest - res: BaseNextResponse + req: BaseNextRequest | NodeNextRequest + res: BaseNextResponse | NodeNextResponse query: ParsedUrlQuery params: Params | undefined page: string