Skip to content

Commit c8b7ba7

Browse files
committed
Add server-sider rendering part to README
1 parent e7ca967 commit c8b7ba7

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ app.use(webpackMiddleware(webpack({
4141
// but it will work with other paths too.
4242
}
4343
}), {
44-
// publicPath is required, whereas all other options are optional
44+
// publicPath is required, whereas all other options are optional
4545

4646
noInfo: false,
4747
// display no info to console (only warnings and errors)
@@ -70,6 +70,9 @@ app.use(webpackMiddleware(webpack({
7070
colors: true
7171
}
7272
// options for formating the statistics
73+
74+
serverSideRender: false,
75+
// Turn off the server-side rendering mode. See Server-Side Rendering part for more info.
7376
}));
7477
```
7578

@@ -109,3 +112,18 @@ This part shows how you might interact with the middleware during runtime:
109112
console.log('Package is in a valid state');
110113
});
111114
```
115+
116+
## Server-Side Rendering
117+
In order to develop a server-side rendering application, we need access to the [`stats`](https://github.com/webpack/docs/wiki/node.js-api#stats), which is generated with the latest build.
118+
119+
In the server-side rendering mode, __webpack-dev-middleware__ sets the `stat` to `res.locals.webpackStats`, then call `next()` to trigger the next middleware, where we can render pages and response to clients. During the webpack building process, all requests will be pending until the build is finished and the `stat` is available.
120+
121+
```JavaScript
122+
app.use(webpackMiddleware(compiler, { serverSideRender: true })
123+
124+
// The following middleware would not be invoked until the latest build is finished.
125+
app.use((req, res) => {
126+
const assetsByChunkName = res.locals.webpackStats.toJson().assetsByChunkName
127+
// then use `assetsByChunkName` for server-sider rendering
128+
})
129+
```

0 commit comments

Comments
 (0)