Skip to content

Commit b77ed04

Browse files
wuctSpaceK33z
authored andcommitted
Fix SSR test (#122)
* Fix server side render test * Enhance SSR section in README
1 parent ca63555 commit b77ed04

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,40 @@ This part shows how you might interact with the middleware during runtime:
119119
## Server-Side Rendering
120120
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.
121121

122-
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.
122+
In the server-side rendering mode, __webpack-dev-middleware__ would sets the `stat` to `res.locals.webpackStats` before invoking the next middleware, where we can render pages and response to clients.
123+
124+
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.
123125

124126
```JavaScript
125127
app.use(webpackMiddleware(compiler, { serverSideRender: true })
126128

127129
// The following middleware would not be invoked until the latest build is finished.
128130
app.use((req, res) => {
129-
const assetsByChunkName = res.locals.webpackStats.toJson().assetsByChunkName
131+
const assetsByChunkName = res.locals.webpackStats.toJson().assetsByChunkName
132+
130133
// then use `assetsByChunkName` for server-sider rendering
134+
// For example, if you have only one main chunk:
135+
136+
res.send(`
137+
<html>
138+
<head>
139+
<title>My App</title>
140+
${
141+
assetsByChunkName.main
142+
.filter(path => path.endsWith('.css'))
143+
.map(path => `<link rel="stylesheet" href="${path}" />`)
144+
}
145+
</head>
146+
<body>
147+
<div id="root"></div>
148+
${
149+
assetsByChunkName.main
150+
.filter(path => path.endsWith('.js'))
151+
.map(path => `<script src="${path}" />`)
152+
}
153+
</body>
154+
</html>
155+
`)
156+
131157
})
132158
```

test/Server.test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,16 @@ describe("Server", function() {
9999
}));
100100
app.use(function(req, res) {
101101
locals = res.locals;
102+
res.sendStatus(200);
102103
});
103104
listen = listenShorthand(done);
104105
});
105106
after(close);
106107

107108
it("request to bundle file", function(done) {
108-
request(app).get("/bundle.js")
109+
request(app).get("/foo/bar")
109110
.expect(200, function() {
110-
// TODO: I would expect `locals` to be set here.
111-
// note that the liner underneath is purely to please the linter
112-
should.strictEqual(locals, undefined);
111+
should.exist(locals.webpackStats);
113112
done();
114113
});
115114
});

0 commit comments

Comments
 (0)