88 parsePathWithSlug ,
99 getPathMapping ,
1010} from '../lib/utils/path.js' ;
11+ import type { BuildConfig } from '../server.js' ;
1112import type { PathSpec } from '../lib/utils/path.js' ;
1213
1314// createPages API (a wrapper around unstable_defineRouter)
@@ -86,19 +87,26 @@ type CreateLayout = <T extends string>(layout: {
8687} ) => void ;
8788
8889export function createPages (
89- fn : ( fns : {
90- createPage : CreatePage ;
91- createLayout : CreateLayout ;
92- } ) => Promise < void > ,
90+ fn : (
91+ fns : {
92+ createPage : CreatePage ;
93+ createLayout : CreateLayout ;
94+ unstable_setBuildData : ( path : string , data : unknown ) => void ;
95+ } ,
96+ opts : {
97+ unstable_buildConfig : BuildConfig | undefined ;
98+ } ,
99+ ) => Promise < void > ,
93100) {
94101 let configured = false ;
95102
96103 // TODO I think there's room for improvement to refactor these structures
97- const staticPathSet = new Set < PathSpec > ( ) ;
104+ const staticPathSet = new Set < [ string , PathSpec ] > ( ) ;
98105 const dynamicPathMap = new Map < string , [ PathSpec , FunctionComponent < any > ] > ( ) ;
99106 const wildcardPathMap = new Map < string , [ PathSpec , FunctionComponent < any > ] > ( ) ;
100107 const staticComponentMap = new Map < string , FunctionComponent < any > > ( ) ;
101108 const noSsrSet = new WeakSet < PathSpec > ( ) ;
109+ const buildDataMap = new Map < string , unknown > ( ) ;
102110
103111 const registerStaticComponent = (
104112 id : string ,
@@ -126,7 +134,7 @@ export function createPages(
126134 ( { type } ) => type === 'wildcard' ,
127135 ) . length ;
128136 if ( page . render === 'static' && numSlugs === 0 ) {
129- staticPathSet . add ( pathSpec ) ;
137+ staticPathSet . add ( [ page . path , pathSpec ] ) ;
130138 const id = joinPath ( page . path , 'page' ) . replace ( / ^ \/ / , '' ) ;
131139 registerStaticComponent ( id , page . component ) ;
132140 } else if ( page . render === 'static' && numSlugs > 0 && numWildcards === 0 ) {
@@ -151,7 +159,10 @@ export function createPages(
151159 }
152160 return name ;
153161 } ) ;
154- staticPathSet . add ( pathItems . map ( ( name ) => ( { type : 'literal' , name } ) ) ) ;
162+ staticPathSet . add ( [
163+ page . path ,
164+ pathItems . map ( ( name ) => ( { type : 'literal' , name } ) ) ,
165+ ] ) ;
155166 const id = joinPath ( ...pathItems , 'page' ) ;
156167 const WrappedComponent = ( props : Record < string , unknown > ) =>
157168 createElement ( page . component as any , { ...props , ...mapping } ) ;
@@ -180,33 +191,66 @@ export function createPages(
180191 registerStaticComponent ( id , layout . component ) ;
181192 } ;
182193
183- const ready = fn ( { createPage, createLayout } ) . then ( ( ) => {
184- configured = true ;
185- } ) ;
194+ const unstable_setBuildData = ( path : string , data : unknown ) => {
195+ buildDataMap . set ( path , data ) ;
196+ } ;
197+
198+ let ready : Promise < void > | undefined ;
199+ const configure = async ( buildConfig ?: BuildConfig ) => {
200+ if ( ! configured && ! ready ) {
201+ ready = fn (
202+ { createPage, createLayout, unstable_setBuildData } ,
203+ { unstable_buildConfig : buildConfig } ,
204+ ) ;
205+ await ready ;
206+ configured = true ;
207+ }
208+ await ready ;
209+ } ;
186210
187211 return defineRouter (
188212 async ( ) => {
189- await ready ;
190- const paths : { path : PathSpec ; isStatic : boolean ; noSsr : boolean } [ ] = [ ] ;
191- for ( const pathSpec of staticPathSet ) {
213+ await configure ( ) ;
214+ const paths : {
215+ path : PathSpec ;
216+ isStatic : boolean ;
217+ noSsr : boolean ;
218+ data : unknown ;
219+ } [ ] = [ ] ;
220+ for ( const [ path , pathSpec ] of staticPathSet ) {
192221 const noSsr = noSsrSet . has ( pathSpec ) ;
193- paths . push ( { path : pathSpec , isStatic : true , noSsr } ) ;
222+ paths . push ( {
223+ path : pathSpec ,
224+ isStatic : true ,
225+ noSsr,
226+ data : buildDataMap . get ( path ) ,
227+ } ) ;
194228 }
195- for ( const [ pathSpec ] of dynamicPathMap . values ( ) ) {
229+ for ( const [ path , [ pathSpec ] ] of dynamicPathMap ) {
196230 const noSsr = noSsrSet . has ( pathSpec ) ;
197- paths . push ( { path : pathSpec , isStatic : false , noSsr } ) ;
231+ paths . push ( {
232+ path : pathSpec ,
233+ isStatic : false ,
234+ noSsr,
235+ data : buildDataMap . get ( path ) ,
236+ } ) ;
198237 }
199- for ( const [ pathSpec ] of wildcardPathMap . values ( ) ) {
238+ for ( const [ path , [ pathSpec ] ] of wildcardPathMap ) {
200239 const noSsr = noSsrSet . has ( pathSpec ) ;
201- paths . push ( { path : pathSpec , isStatic : false , noSsr } ) ;
240+ paths . push ( {
241+ path : pathSpec ,
242+ isStatic : false ,
243+ noSsr,
244+ data : buildDataMap . get ( path ) ,
245+ } ) ;
202246 }
203247 return paths ;
204248 } ,
205- async ( id , setShouldSkip ) => {
206- await ready ;
249+ async ( id , { unstable_setShouldSkip , unstable_buildConfig } ) => {
250+ await configure ( unstable_buildConfig ) ;
207251 const staticComponent = staticComponentMap . get ( id ) ;
208252 if ( staticComponent ) {
209- setShouldSkip ( { } ) ;
253+ unstable_setShouldSkip ( { } ) ;
210254 return staticComponent ;
211255 }
212256 for ( const [ pathSpec , Component ] of dynamicPathMap . values ( ) ) {
@@ -216,12 +260,12 @@ export function createPages(
216260 ) ;
217261 if ( mapping ) {
218262 if ( Object . keys ( mapping ) . length === 0 ) {
219- setShouldSkip ( ) ;
263+ unstable_setShouldSkip ( ) ;
220264 return Component ;
221265 }
222266 const WrappedComponent = ( props : Record < string , unknown > ) =>
223267 createElement ( Component , { ...props , ...mapping } ) ;
224- setShouldSkip ( ) ;
268+ unstable_setShouldSkip ( ) ;
225269 return WrappedComponent ;
226270 }
227271 }
@@ -233,11 +277,11 @@ export function createPages(
233277 if ( mapping ) {
234278 const WrappedComponent = ( props : Record < string , unknown > ) =>
235279 createElement ( Component , { ...props , ...mapping } ) ;
236- setShouldSkip ( ) ;
280+ unstable_setShouldSkip ( ) ;
237281 return WrappedComponent ;
238282 }
239283 }
240- setShouldSkip ( { } ) ; // negative cache
284+ unstable_setShouldSkip ( { } ) ; // negative cache
241285 return null ; // not found
242286 } ,
243287 ) ;
0 commit comments