@@ -215,8 +215,10 @@ export default class WithReact {
215215 * Defines a new layout
216216 */
217217 layout ( name : string , file : string ) : WithReact {
218- this . _layouts [ name ] = file
219- return this
218+ const resolved = this . _application . withVirtualFS . resolvePath ( file ) ;
219+ Exception . require ( ! ! resolved , 'Module not found %s' , file ) ;
220+ this . _layouts [ name ] = resolved as string ;
221+ return this ;
220222 }
221223
222224 /**
@@ -264,51 +266,66 @@ export default class WithReact {
264266 render ( pathname : string ) : string {
265267 //get router props
266268 const routerProps = { location : pathname , context : { } } ;
267-
268- const page = this . _page . clone ;
269-
269+ //make app props
270+ const appProps : Record < string , any > = { } ;
271+ for ( const key in this . _props ) {
272+ const prop = require ( this . _props [ key ] ) ;
273+ appProps [ key ] = prop . default || prop ;
274+ }
275+ //make a chunk extractor config
270276 const chunkConfig = {
271277 statsFile : path . join ( this . _application . buildPath , 'static/stats.json' ) ,
272278 publicPath : this . _application . buildURL
273279 } as ChunkExtractorOptions ;
274280
275- //get the path match
281+ //see if we can find a matching route for this path
276282 const match = this . match ( pathname ) ;
277283 if ( typeof match === 'string' ) {
284+ //we only need to load the active entry point
285+ //to the `ChunkExtractor` on the server
278286 chunkConfig . entrypoints = [ this . entryFileName ( match ) ] ;
287+ //we only need to load the active route
288+ //to the app props on the server
289+ const view = require ( this . _routes [ match ] . view ) ;
290+ const layout = require ( this . _layouts [ this . _routes [ match ] . layout ] ) ;
291+ appProps . routes = [ {
292+ path : this . _routes [ match ] . path ,
293+ view : view . default || view ,
294+ layout : layout . default || layout
295+ } ] ;
279296 }
280297
281298 //now do the loadable chunking thing..
282299 //see: https://loadable-components.com/docs/server-side-rendering/
283300 const extractor = new ChunkExtractor ( chunkConfig ) ;
284301
285- //wrap the app
302+ //wrap the app with the server router (vs BrowserRouter)
286303 const App = require ( this . _app ) ;
287304 const Router = React . createElement (
288305 StaticRouter ,
289306 routerProps ,
290- React . createElement ( App . default || App )
307+ React . createElement ( App . default || App , appProps )
291308 ) ;
292309 //render the app now
293310 const app = ReactDOMServer . renderToString (
294311 extractor . collectChunks ( Router )
295312 ) ;
296313
314+ //clone the page
315+ const page = this . _page . clone ;
297316 //add links to head
298317 extractor . getLinkElements ( ) . forEach ( link => {
299318 page . head . addChild ( link ) ;
300319 } ) ;
301-
302320 //add styles to head
303321 extractor . getStyleElements ( ) . forEach ( style => {
304322 page . head . addChild ( style ) ;
305323 } ) ;
306-
307324 //add scripts to body
308325 extractor . getScriptElements ( ) . forEach ( script => {
309326 page . body . addChild ( script ) ;
310327 } ) ;
311-
328+ //render the page
312329 return page . render ( app ) ;
313330 }
314331}
0 commit comments