2017-04-07 17:58:35 +00:00
|
|
|
import webpackHotMiddlewareClient from 'webpack-hot-middleware/client?overlay=false&reload=true&path=/_next/webpack-hmr'
|
2016-12-19 14:40:26 +00:00
|
|
|
import Router from '../lib/router'
|
2016-11-23 18:32:49 +00:00
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
export default () => {
|
|
|
|
const handlers = {
|
|
|
|
reload (route) {
|
|
|
|
if (route === '/_error') {
|
|
|
|
for (const r of Object.keys(Router.components)) {
|
|
|
|
const { err } = Router.components[r]
|
|
|
|
if (err) {
|
|
|
|
// reload all error routes
|
|
|
|
// which are expected to be errors of '/_error' routes
|
|
|
|
Router.reload(r)
|
|
|
|
}
|
2016-11-23 18:32:49 +00:00
|
|
|
}
|
2017-04-18 04:18:43 +00:00
|
|
|
return
|
2016-11-23 18:32:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
if (route === '/_document') {
|
|
|
|
window.location.reload()
|
|
|
|
return
|
|
|
|
}
|
2016-12-27 23:28:19 +00:00
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
Router.reload(route)
|
|
|
|
},
|
2016-12-27 23:28:19 +00:00
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
change (route) {
|
|
|
|
if (route === '/_document') {
|
|
|
|
window.location.reload()
|
|
|
|
return
|
|
|
|
}
|
2016-12-27 23:28:19 +00:00
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
const { err } = Router.components[route] || {}
|
|
|
|
if (err) {
|
|
|
|
// reload to recover from runtime errors
|
|
|
|
Router.reload(route)
|
|
|
|
}
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
2016-11-23 18:32:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
webpackHotMiddlewareClient.subscribe((obj) => {
|
|
|
|
const fn = handlers[obj.action]
|
|
|
|
if (fn) {
|
|
|
|
const data = obj.data || []
|
|
|
|
fn(...data)
|
|
|
|
} else {
|
|
|
|
throw new Error('Unexpected action ' + obj.action)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|