2017-10-14 19:20:49 +02:00
|
|
|
import initNext, * as next from './'
|
2019-02-22 16:23:58 +01:00
|
|
|
import EventSourcePolyfill from './event-source-polyfill'
|
2017-04-18 06:18:43 +02:00
|
|
|
import initOnDemandEntries from './on-demand-entries-client'
|
|
|
|
import initWebpackHMR from './webpack-hot-middleware-client'
|
2018-01-30 16:40:52 +01:00
|
|
|
|
2018-07-26 12:38:45 +02: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 22:54:18 +01:00
|
|
|
// REPLACE_NOOP_IMPORT
|
2018-07-26 12:38:45 +02:00
|
|
|
|
2019-02-22 16:23:58 +01:00
|
|
|
// Support EventSource on Internet Explorer 11
|
|
|
|
if (!window.EventSource) {
|
|
|
|
window.EventSource = EventSourcePolyfill
|
|
|
|
}
|
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
const {
|
|
|
|
__NEXT_DATA__: {
|
|
|
|
assetPrefix
|
|
|
|
}
|
|
|
|
} = window
|
|
|
|
|
|
|
|
const prefix = assetPrefix || ''
|
2019-02-19 22:45:07 +01:00
|
|
|
const webpackHMR = initWebpackHMR({ assetPrefix: prefix })
|
2017-01-12 02:58:20 +01:00
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
window.next = next
|
|
|
|
initNext({ webpackHMR })
|
2017-04-18 06:18:43 +02:00
|
|
|
.then((emitter) => {
|
2019-02-19 22:45:07 +01:00
|
|
|
initOnDemandEntries({ assetPrefix: prefix })
|
2017-04-18 06:18:43 +02: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 11:24:40 +02:00
|
|
|
}).catch((err) => {
|
|
|
|
console.error('Error was not caught', err)
|
2017-04-18 06:18:43 +02:00
|
|
|
})
|