Gathering detailed insights and metrics for webpack-hot-server-middleware
Gathering detailed insights and metrics for webpack-hot-server-middleware
Gathering detailed insights and metrics for webpack-hot-server-middleware
Gathering detailed insights and metrics for webpack-hot-server-middleware
webpack-hot-middleware
Webpack hot reloading you can attach to your own server
@gatsbyjs/webpack-hot-middleware
Webpack hot reloading you can attach to your own server
webpack-dev-middleware
A development middleware for webpack
webpack-dev-server
Serves a webpack app. Updates the browser on changes.
npm install webpack-hot-server-middleware
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
323 Stars
36 Commits
50 Forks
7 Watching
34 Branches
15 Contributors
Updated on 10 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
12.7%
3,534
Compared to previous day
Last week
1%
15,764
Compared to previous week
Last month
8.3%
70,055
Compared to previous month
Last year
5%
809,713
Compared to previous year
Webpack Hot Server Middleware is designed to be used in conjunction with webpack-dev-middleware
(and optionally webpack-hot-middleware
) to hot update Webpack bundles on the server.
When creating universal Web apps it's common to build two bundles with Webpack, one client bundle targeting 'web' and another server bundle targeting 'node'.
The entry point to the client bundle renders to the DOM, e.g.
1// client.js 2 3import ReactDOM from 'react-dom'; 4import App from './components/App'; 5 6ReactDOM.render(<App />, document.getElementById('root'));
And the entry point to the server bundle renders to string, e.g.
1// server.js 2 3import { renderToString } from 'react-dom/server'; 4import App from './components/App'; 5 6export default function serverRenderer() { 7 return (req, res, next) => { 8 res.status(200).send(` 9 <!doctype html> 10 <html> 11 <head> 12 <title>App</title> 13 </head> 14 <body> 15 <div id="root"> 16 ${renderToString(<App />)} 17 </div> 18 <script src="/client.js"></script> 19 </body> 20 </html> 21 `); 22 }; 23}
NOTE: The server bundle is itself middleware allowing you to mount it anywhere in an existing node server, e.g.
1const express = require('express'); 2const serverRenderer = require('./dist/server'); 3const app = express(); 4 5app.use(serverRenderer()); 6app.listen(6060);
Given this setup it's fairly easy to hook up hot module replacement for your client bundle using webpack-dev-server
or webpack-hot-middleware
however these middlewares don't handle server bundles meaning you need to frequently restart your server to see the latest changes.
Webpack Hot Server Middleware solves this problem, ensuring the server bundle used is always the latest compilation without requiring a restart. Additionally it allows your client and server bundle to share the same Webpack cache for faster builds and uses an in-memory bundle on the server to avoid hitting the disk.
It turns out hot module replacement is much easier on the server than on the client as you don't have any state to preserve because middleware is almost always necessarily stateless, so the entire bundle can be replaced at the top level whenever a change occurs.
Webpack Hot Server Middleware expects your Webpack config to export an array of configurations, one for your client bundle and one for your server bundle, e.g.
1// webpack.config.js 2 3module.exports = [ 4 { 5 name: 'client', 6 target: 'web', 7 entry: './client.js' 8 ... 9 }, { 10 name: 'server', 11 target: 'node', 12 entry: './server.js' 13 ... 14 } 15];
NOTE: It's important both the 'client' and 'server' configs are given a name prefixed with 'client' and 'server' respectively.
It then needs to be mounted immediately after webpack-dev-middleware
, e.g.
1const express = require('express'); 2const webpack = require('webpack'); 3const webpackDevMiddleware = require('webpack-dev-middleware'); 4const webpackHotServerMiddleware = require('webpack-hot-server-middleware'); 5const config = require('./webpack.config.js'); 6const app = express(); 7 8const compiler = webpack(config); 9 10app.use(webpackDevMiddleware(compiler, { 11 serverSideRender: true 12})); 13app.use(webpackHotServerMiddleware(compiler)); 14 15app.listen(6060);
Now whenever Webpack rebuilds, the new bundle will be used both client and server side.
webpackHotServerMiddleware (compiler: MultiCompiler, options?: Options) => void
chunkName string
The name of the server entry point, defaults to 'main'.
serverRendererOptions object
Mixed in with clientStats
& serverStats
and passed to the serverRenderer
.
A simple example can be found in the example directory and a more real world example can be seen in the 60fram.es boilerplate.
webpack-hot-middleware
webpack-hot-middleware
needs to be mounted before webpack-hot-server-middleware
to ensure client hot module replacement requests are handled correctly, e.g.
1const express = require('express'); 2const webpack = require('webpack'); 3const webpackDevMiddleware = require('webpack-dev-middleware'); 4const webpackHotMiddleware = require('webpack-hot-middleware'); 5const webpackHotServerMiddleware = require('webpack-hot-server-middleware'); 6const config = require('./webpack.config.js'); 7const app = express(); 8 9const compiler = webpack(config); 10 11app.use(webpackDevMiddleware(compiler, { 12 serverSideRender: true 13})); 14// NOTE: Only the client bundle needs to be passed to `webpack-hot-middleware`. 15app.use(webpackHotMiddleware(compiler.compilers.find(compiler => compiler.name === 'client'))); 16app.use(webpackHotServerMiddleware(compiler)); 17 18app.listen(6060);
A production setup might conditionally use express.static
instead of webpack-dev-server
and a pre-built server bundle instead of webpack-hot-server-middleware
, e.g.
1const express = require('express'); 2const path = require('path'); 3const app = express(); 4 5if (process.env.NODE_ENV !== 'production') { 6 const webpack = require('webpack'); 7 const webpackDevMiddleware = require('webpack-dev-middleware'); 8 const webpackHotMiddleware = require('webpack-hot-middleware'); 9 const webpackHotServerMiddleware = require('webpack-hot-server-middleware'); 10 const config = require('./webpack.config.js'); 11 const compiler = webpack(config); 12 app.use(webpackDevMiddleware(compiler, { 13 serverSideRender: true 14 })); 15 app.use(webpackHotMiddleware(compiler.compilers.find(compiler => compiler.name === 'client'))); 16 app.use(webpackHotServerMiddleware(compiler)); 17} else { 18 const CLIENT_ASSETS_DIR = path.join(__dirname, '../build/client'); 19 const CLIENT_STATS_PATH = path.join(CLIENT_ASSETS_DIR, 'stats.json'); 20 const SERVER_RENDERER_PATH = path.join(__dirname, '../build/server.js'); 21 const serverRenderer = require(SERVER_RENDERER_PATH); 22 const stats = require(CLIENT_STATS_PATH); 23 app.use(express.static(CLIENT_ASSETS_DIR)); 24 app.use(serverRenderer(stats)); 25} 26 27app.listen(6060);
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 14/30 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
107 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More