Skip to content

Commit ebb1fe5

Browse files
GeorgeTaveras1231shellscape
authored andcommitted
Make serverSideRendering example work consistently (#196)
* Make serverSideRendering example work consistently * Fix interpolation in serverSideRendering example * Fix script tags
1 parent 26f0b40 commit ebb1fe5

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,39 @@ In the server-side rendering mode, __webpack-dev-middleware__ would sets the `st
137137
Notice that requests for bundle files would still be responded by __webpack-dev-middleware__ and all requests will be pending until the building process is finished in the server-side rendering mode.
138138

139139
```javascript
140+
// This function makes server rendering of asset references consistent with different webpack chunk/entry confiugrations
141+
function normalizeAssets(assets) {
142+
return Array.isArray(assets) ? assets : [assets]
143+
}
144+
140145
app.use(webpackMiddleware(compiler, { serverSideRender: true })
141146

142147
// The following middleware would not be invoked until the latest build is finished.
143148
app.use((req, res) => {
149+
144150
const assetsByChunkName = res.locals.webpackStats.toJson().assetsByChunkName
145-
151+
146152
// then use `assetsByChunkName` for server-sider rendering
147-
// For example, if you have only one main chunk:
153+
// For example, if you have only one main chunk:
148154

149155
res.send(`
150156
<html>
151157
<head>
152158
<title>My App</title>
153159
${
154-
assetsByChunkName.main
160+
normalizeAssets(assetsByChunkName.main)
155161
.filter(path => path.endsWith('.css'))
156162
.map(path => `<link rel="stylesheet" href="${path}" />`)
163+
.join('\n')
157164
}
158165
</head>
159166
<body>
160167
<div id="root"></div>
161168
${
162-
assetsByChunkName.main
169+
normalizeAssets(assetsByChunkName.main)
163170
.filter(path => path.endsWith('.js'))
164-
.map(path => `<script src="${path}" />`)
171+
.map(path => `<script src="${path}"></script>`)
172+
.join('\n')
165173
}
166174
</body>
167175
</html>

0 commit comments

Comments
 (0)