Skip to content

Instantly share code, notes, and snippets.

@kettanaito
Created July 20, 2019 17:34
Show Gist options
  • Save kettanaito/cbb7cd4eeca6a0c377e777501996f038 to your computer and use it in GitHub Desktop.
Save kettanaito/cbb7cd4eeca6a0c377e777501996f038 to your computer and use it in GitHub Desktop.
Webpack server HMR issue
import express from 'express'
const app = express()
app.use((req, res, next) => {
console.log('ping')
next()
})
export default app
import http from 'http'
import app from './app'
let currentApp = app
const server = http.createServer(currentApp)
server.listen(8080, () => console.log('Server established')
if (module.hot) {
// Whenever hot update happens in "app"
module.hot.accepts(['./app'], () => {
// Stop serving the outdated app
server.removeListener('request', currentApp)
// Require the updated app module
const app = require('./app').default
// Start serving the updated app
server.on('request', app)
currentApp = app
})
}
@kettanaito
Copy link
Author

kettanaito commented Jul 20, 2019

I also use "start-server-webpack-plugin": "^2.2.5" that spawns an actual built server automatically and signals updates.

The setup above provides hot module replacement for the server, but appends multiple instances of the same app middleware upon hot updates. Each update appends a new middleware, resulting into:

middlewareCount = updates + 1

Is there something wrong with this hot update declaration? How to ensure that app is thrown away and re-created upon hot update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment