Skip to content

Commit

Permalink
feat(middleware): expose the memory filesystem (response.locals.fs) (
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdinando-ferreira authored and michael-ciniawsky committed Sep 7, 2018
1 parent bf586c9 commit f9a138e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ In order to develop an app using server-side rendering, we need access to the
generated with each build.

With server-side rendering enabled, `webpack-dev-middleware` sets the `stat` to
`res.locals.webpackStats` before invoking the next middleware, allowing a
`res.locals.webpackStats` and the memory filesystem to `res.locals.fs` before invoking the next middleware, allowing a
developer to render the page body and manage the response to clients.

_Note: Requests for bundle files will still be handled by
Expand Down Expand Up @@ -337,17 +337,21 @@ app.use(middleware(compiler, { serverSideRender: true }))
// The following middleware would not be invoked until the latest build is finished.
app.use((req, res) => {
const assetsByChunkName = res.locals.webpackStats.toJson().assetsByChunkName
const fs = res.locals.fs
const outputPath = res.locals.webpackStats.toJson().outputPath

// then use `assetsByChunkName` for server-sider rendering
// For example, if you have only one main chunk:
res.send(`
<html>
<head>
<title>My App</title>
<style>
${normalizeAssets(assetsByChunkName.main)
.filter(path => path.endsWith('.css'))
.map(path => `<link rel="stylesheet" href="${path}" />`)
.map(path => fs.readFileSync(outputPath + '/' + path))
.join('\n')}
</style>
</head>
<body>
<div id="root"></div>
Expand Down
1 change: 1 addition & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = function wrapper(context) {
return new Promise(((resolve) => {
ready(context, () => {
res.locals.webpackStats = context.webpackStats;
res.locals.fs = context.fs;
resolve(next());
}, req);
}));
Expand Down
1 change: 1 addition & 0 deletions test/tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ describe('Server', () => {
request(app).get('/foo/bar')
.expect(200, () => {
assert(locals.webpackStats);
assert(locals.fs);
done();
});
});
Expand Down

0 comments on commit f9a138e

Please sign in to comment.