Created
July 20, 2019 17:34
-
-
Save kettanaito/cbb7cd4eeca6a0c377e777501996f038 to your computer and use it in GitHub Desktop.
Webpack server HMR issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express' | |
const app = express() | |
app.use((req, res, next) => { | |
console.log('ping') | |
next() | |
}) | |
export default app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Is there something wrong with this hot update declaration? How to ensure that app is thrown away and re-created upon hot update?