2017-10-14 17:20:49 +00:00
|
|
|
import initNext, * as next from './'
|
2017-04-18 04:18:43 +00:00
|
|
|
import initOnDemandEntries from './on-demand-entries-client'
|
|
|
|
import initWebpackHMR from './webpack-hot-middleware-client'
|
2018-01-30 15:40:52 +00:00
|
|
|
|
2018-07-26 10:38:45 +00:00
|
|
|
// Temporary workaround for the issue described here:
|
|
|
|
// https://github.com/zeit/next.js/issues/3775#issuecomment-407438123
|
|
|
|
// The runtimeChunk doesn't have dynamic import handling code when there hasn't been a dynamic import
|
|
|
|
// The runtimeChunk can't hot reload itself currently to correct it when adding pages using on-demand-entries
|
2019-01-11 21:54:18 +00:00
|
|
|
// REPLACE_NOOP_IMPORT
|
2018-07-26 10:38:45 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
const {
|
|
|
|
__NEXT_DATA__: {
|
|
|
|
assetPrefix
|
|
|
|
}
|
|
|
|
} = window
|
|
|
|
|
|
|
|
const prefix = assetPrefix || ''
|
|
|
|
const webpackHMR = initWebpackHMR({assetPrefix: prefix})
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
window.next = next
|
|
|
|
initNext({ webpackHMR })
|
2017-04-18 04:18:43 +00:00
|
|
|
.then((emitter) => {
|
2018-07-24 09:24:40 +00:00
|
|
|
initOnDemandEntries({assetPrefix: prefix})
|
2017-04-18 04:18:43 +00:00
|
|
|
|
|
|
|
let lastScroll
|
|
|
|
|
|
|
|
emitter.on('before-reactdom-render', ({ Component, ErrorComponent }) => {
|
|
|
|
// Remember scroll when ErrorComponent is being rendered to later restore it
|
|
|
|
if (!lastScroll && Component === ErrorComponent) {
|
|
|
|
const { pageXOffset, pageYOffset } = window
|
|
|
|
lastScroll = {
|
|
|
|
x: pageXOffset,
|
|
|
|
y: pageYOffset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
emitter.on('after-reactdom-render', ({ Component, ErrorComponent }) => {
|
|
|
|
if (lastScroll && Component !== ErrorComponent) {
|
|
|
|
// Restore scroll after ErrorComponent was replaced with a page component by HMR
|
|
|
|
const { x, y } = lastScroll
|
|
|
|
window.scroll(x, y)
|
|
|
|
lastScroll = null
|
|
|
|
}
|
|
|
|
})
|
2018-07-24 09:24:40 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error('Error was not caught', err)
|
2017-04-18 04:18:43 +00:00
|
|
|
})
|