@@ -5,6 +5,7 @@ import type { EdgeAppRouteLoaderQuery } from './webpack/loaders/next-edge-app-ro
55import type { NextConfigComplete } from '../server/config-shared'
66import type { webpack } from 'next/dist/compiled/webpack/webpack'
77import type {
8+ AppPageStaticInfo ,
89 MiddlewareConfig ,
910 MiddlewareMatcher ,
1011 PageStaticInfo ,
@@ -76,6 +77,7 @@ import type { PageExtensions } from './page-extensions-type'
7677import type { MappedPages } from './build-context'
7778import { PAGE_TYPES } from '../lib/page-types'
7879import { isAppPageRoute } from '../lib/is-app-page-route'
80+ import { getParamsFromLayoutFilePath } from './webpack/loaders/next-root-params-loader'
7981
8082export function sortByPageExts ( pageExtensions : PageExtensions ) {
8183 return ( a : string , b : string ) => {
@@ -133,25 +135,26 @@ export async function getStaticInfoIncludingLayouts({
133135
134136 const segments = [ pageStaticInfo ]
135137
136- // inherit from layout files only if it's a page route
137- if ( isAppPageRoute ( page ) ) {
138- const layoutFiles = [ ]
139- const potentialLayoutFiles = pageExtensions . map ( ( ext ) => 'layout.' + ext )
140- let dir = dirname ( pageFilePath )
141-
142- // Uses startsWith to not include directories further up.
143- while ( dir . startsWith ( appDir ) ) {
144- for ( const potentialLayoutFile of potentialLayoutFiles ) {
145- const layoutFile = join ( dir , potentialLayoutFile )
146- if ( ! fs . existsSync ( layoutFile ) ) {
147- continue
148- }
149- layoutFiles . push ( layoutFile )
138+ const layoutFiles : string [ ] = [ ]
139+ const potentialLayoutFiles = pageExtensions . map ( ( ext ) => 'layout.' + ext )
140+ let dir = dirname ( pageFilePath )
141+
142+ // We need to find the root layout for both pages and route handlers.
143+ // Uses startsWith to not include directories further up.
144+ while ( dir . startsWith ( appDir ) ) {
145+ for ( const potentialLayoutFile of potentialLayoutFiles ) {
146+ const layoutFile = join ( dir , potentialLayoutFile )
147+ if ( ! fs . existsSync ( layoutFile ) ) {
148+ continue
150149 }
151- // Walk up the directory tree
152- dir = join ( dir , '..' )
150+ layoutFiles . push ( layoutFile )
153151 }
152+ // Walk up the directory tree
153+ dir = join ( dir , '..' )
154+ }
154155
156+ // inherit from layout files only if it's a page route
157+ if ( isAppPageRoute ( page ) ) {
155158 for ( const layoutFile of layoutFiles ) {
156159 const layoutStaticInfo = await getAppPageStaticInfo ( {
157160 nextConfig,
@@ -165,6 +168,11 @@ export async function getStaticInfoIncludingLayouts({
165168 }
166169 }
167170
171+ const rootLayout = layoutFiles . at ( - 1 )
172+ const rootParams = rootLayout
173+ ? getParamsFromLayoutFilePath ( { appDir, layoutFilePath : rootLayout } )
174+ : [ ]
175+
168176 const config = reduceAppConfig ( segments )
169177
170178 return {
@@ -173,6 +181,7 @@ export async function getStaticInfoIncludingLayouts({
173181 runtime : config . runtime ,
174182 preferredRegion : config . preferredRegion ,
175183 maxDuration : config . maxDuration ,
184+ rootParams,
176185 }
177186}
178187
@@ -709,6 +718,7 @@ export async function createEntrypoints(
709718 page,
710719 name : serverBundlePath ,
711720 pagePath : absolutePagePath ,
721+ rootParams : ( staticInfo as AppPageStaticInfo ) . rootParams ! ,
712722 appDir,
713723 appPaths : matchedAppPaths ,
714724 pageExtensions,
@@ -787,6 +797,7 @@ export async function createEntrypoints(
787797 name : serverBundlePath ,
788798 page,
789799 pagePath : absolutePagePath ,
800+ rootParams : ( staticInfo as AppPageStaticInfo ) . rootParams ! ,
790801 appDir : appDir ! ,
791802 appPaths : matchedAppPaths ,
792803 pageExtensions,
0 commit comments