Monday 5 June 2017

Hot reload and preserve state with express server webpack bundle

My app is divided in 2 parts, client and server. On client side I use webpack-dev-server, react-hot-reload and I bundle everything with webpack. On server side I use different server, webpack config and make a second bundle. Since HMR is not related to webpack-dev-server I was able to make it work with server bundle.

const server = http.createServer(app)
let currentApp = app
server.listen(PORT, () => {
  console.log(`Express server is up on port ${PORT}`)
})

if (module.hot) {
  module.hot.accept('./index', () => {
    server.removeListener('request', currentApp)
    server.on('request', app)
    currentApp = app
  })
}

The problem is that when I change something inside routes or templates on server side I have to reload the browser to see the changes (not restart the server, since HMR is active). How can I get changes to inject itself and preserve state not having to refresh the browser, just like on the client side?



via Igor-Vuk

No comments:

Post a Comment