Skip to content

Commit 54b2d0e

Browse files
committed
ssr working without warning
1 parent 0b74f87 commit 54b2d0e

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

packages/incept/src/react/index.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/incept/src/webpack/defaults.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export default {
5757
]
5858
},
5959
optimization: {
60+
moduleIds: 'named',
61+
chunkIds: 'named',
6062
splitChunks: {
6163
chunks: 'all'
6264
}

packages/incept/src/webpack/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ export default class WithWebpack {
102102
//compiling the same thing twice and could be mistaken for a bug.
103103
//all this does is make sure when the compile is complete it is
104104
//logged once. TODO: Find a better solution...
105-
let done = false
105+
let done = false;
106106
bundler.on('display-ready', 'done', (stats: Stats) => {
107-
if (done) return
108-
const time = stats.compilation.endTime - stats.compilation.startTime
109-
console.log(`compiled in ${time}ms`)
110-
done = true
107+
if (done) return;
108+
const time = stats.compilation.endTime - stats.compilation.startTime;
109+
console.log(`compiled in ${time}ms`);
110+
done = true;
111111
});
112112

113113
//add routes as bundler entries

0 commit comments

Comments
 (0)