Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

webpack-hot-server-middleware

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webpack-hot-server-middleware

Hot updates Webpack bundles on the server

  • 0.0.4
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Webpack Hot Server Middleware Build Status npm version

What?

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.

Why?

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.

// client.js

import ReactDOM from 'react-dom';
import App from './components/App';

ReactDOM.render(<App />, document.getElementById('root'));

And the entry point to the server bundle renders to string, e.g.

// server.js

import { renderToString } from 'react-dom/server';
import App from './components/App';

export default function serverRenderer() {
    return (req, res, next) => {
        res.status(200).send(`
            <!doctype html>
            <html>
            <head>
                <title>App</title>
            </head>
            <body>
                <div id="root">
                    ${renderToString(<App />)}
                </div>
                <script src="/client.js"></script>
            </body>
            </html>
        `);
    };
}

NOTE: The server bundle is itself middleware allowing you to mount it anywhere in an existing node server, e.g.

const express = require('express');
const serverRenderer = require('./dist/server');
const app = express();

app.use(serverRenderer());
app.listen(6060);

Given this setup it's fairly easy to hook up hot module reloading for your client bundle using webpack-dev-server or webpack-hot-middleware however these middlewares don't handle server bundles meaning you need to constantly restart your server whenever a change is made.

Webpack Hot Server Middleware will ensure that the server bundle used is always the latest compilation without requiring a restart.

How?

It turns out hot reloading a Webpack bundle on the server is much easier than on the client as you don't have any state to preserve because renderToString is neccesarily stateless so the entire bundle can be replaced at the top level whenever a change occurs.

Usage

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.

// webpack.config.js

module.exports = [
    {
        name: 'client',
        target: 'web',
        entry: './client.js'
        ...
    }, {
        name: 'server',
        target: 'node',
        entry: './server.js'
        ...
    }
];

NOTE: It's important both the 'client' and 'server' configs are given the name 'client' and 'server' respectively.

It then needs to be mounted immediately after webpack-dev-middleware, e.g.

const express = require('express');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotServerMiddleware = require('webpack-hot-server-middleware');
const config = require('./webpack.config.js');
const app = express();

const compiler = webpack(config);

app.use(webpackDevMiddleware(compiler));
app.use(webpackHotServerMiddleware(compiler));

app.listen(6060);

Now whenever Webpack rebuilds the new bundle will be used both client and server side.

Example

A simple example can be found in the example directory and a more real world example can be seen in the 60fram.es boilerplate

License

MIT

FAQs

Package last updated on 02 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc