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
|
|
|
|
|
|
|
const handlers = {
|
|
|
|
reload (route) {
|
|
|
|
if (route === '/_error') {
|
2016-12-19 14:40:26 +00:00
|
|
|
for (const r of Object.keys(Router.components)) {
|
2017-01-12 01:58:20 +00:00
|
|
|
const { err } = Router.components[r]
|
|
|
|
if (err) {
|
|
|
|
// reload all error routes
|
2016-11-23 18:32:49 +00:00
|
|
|
// which are expected to be errors of '/_error' routes
|
2016-12-19 14:40:26 +00:00
|
|
|
Router.reload(r)
|
2016-11-23 18:32:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-27 23:28:19 +00:00
|
|
|
if (route === '/_document') {
|
|
|
|
window.location.reload()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:40:26 +00:00
|
|
|
Router.reload(route)
|
2016-11-24 14:03:16 +00:00
|
|
|
},
|
2016-12-27 23:28:19 +00:00
|
|
|
|
2016-11-24 14:03:16 +00:00
|
|
|
change (route) {
|
2016-12-27 23:28:19 +00:00
|
|
|
if (route === '/_document') {
|
|
|
|
window.location.reload()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
const { err } = Router.components[route] || {}
|
|
|
|
if (err) {
|
2016-11-24 14:03:16 +00:00
|
|
|
// reload to recover from runtime errors
|
2016-12-19 14:40:26 +00:00
|
|
|
Router.reload(route)
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
2016-11-23 18:32:49 +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)
|
|
|
|
}
|
|
|
|
})
|