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-05-25 16:29:03 +00:00
|
|
|
const { err, Component } = Router.components[route] || {}
|
|
|
|
|
2017-07-18 07:30:25 +00:00
|
|
|
if (err) {
|
|
|
|
// reload to recover from runtime errors
|
|
|
|
Router.reload(route)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Router.route !== route) {
|
|
|
|
// If this is a not a change for a currently viewing page.
|
|
|
|
// We don't need to worry about it.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-25 16:29:03 +00:00
|
|
|
if (!Component) {
|
|
|
|
// This only happens when we create a new page without a default export.
|
|
|
|
// If you removed a default export from a exising viewing page, this has no effect.
|
|
|
|
console.log(`Hard reloading due to no default component in page: ${route}`)
|
|
|
|
window.location.reload()
|
2017-04-18 04:18:43 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|