2018-03-06 11:55:03 +00:00
|
|
|
import 'event-source-polyfill'
|
2018-07-24 09:24:40 +00:00
|
|
|
import connect from './dev-error-overlay/hot-dev-client'
|
2016-12-19 14:40:26 +00:00
|
|
|
import Router from '../lib/router'
|
2016-11-23 18:32:49 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
const handlers = {
|
|
|
|
reload (route) {
|
2018-09-11 18:03:20 +00:00
|
|
|
// If the App component changes we have to reload the current route, this is handled by hot-self-accept-loader
|
|
|
|
// So we just return
|
|
|
|
if (route === '/_app') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
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
|
|
|
}
|
2018-04-12 08:33:22 +00:00
|
|
|
}
|
2018-07-24 09:24:40 +00:00
|
|
|
return
|
|
|
|
}
|
2018-04-12 08:33:22 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
// Since _document is server only we need to reload the full page when it changes.
|
|
|
|
if (route === '/_document') {
|
|
|
|
window.location.reload()
|
|
|
|
return
|
|
|
|
}
|
2016-12-27 23:28:19 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
Router.reload(route)
|
|
|
|
},
|
2018-04-12 08:33:22 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
change (route) {
|
2018-09-11 18:03:20 +00:00
|
|
|
// If the App component changes we have to reload the current route, this is handled by hot-self-accept-loader
|
|
|
|
// So we just return
|
2018-07-24 09:24:40 +00:00
|
|
|
if (route === '/_app') {
|
|
|
|
return
|
|
|
|
}
|
2016-12-27 23:28:19 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
const { err, Component } = Router.components[route] || {}
|
2017-05-25 16:29:03 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
if (err) {
|
|
|
|
// reload to recover from runtime errors
|
|
|
|
Router.reload(route)
|
|
|
|
}
|
2017-07-18 07:30:25 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
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-07-18 07:30:25 +00:00
|
|
|
|
2018-07-24 09:24:40 +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.warn(`Hard reloading due to no default component in page: ${route}`)
|
|
|
|
window.location.reload()
|
2016-11-24 14:03:16 +00:00
|
|
|
}
|
2016-11-23 18:32:49 +00:00
|
|
|
}
|
2018-07-24 09:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ({assetPrefix}) => {
|
|
|
|
const options = {
|
|
|
|
path: `${assetPrefix}/_next/webpack-hmr`
|
|
|
|
}
|
|
|
|
|
|
|
|
const devClient = connect(options)
|
2016-11-23 18:32:49 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
devClient.subscribeToHmrEvent((obj) => {
|
2017-04-18 04:18:43 +00:00
|
|
|
const fn = handlers[obj.action]
|
|
|
|
if (fn) {
|
|
|
|
const data = obj.data || []
|
|
|
|
fn(...data)
|
|
|
|
} else {
|
|
|
|
throw new Error('Unexpected action ' + obj.action)
|
|
|
|
}
|
|
|
|
})
|
2018-07-24 09:24:40 +00:00
|
|
|
|
|
|
|
return devClient
|
2017-04-18 04:18:43 +00:00
|
|
|
}
|