2017-10-21 10:58:59 +00:00
|
|
|
import stripAnsi from 'strip-ansi'
|
2017-10-14 17:20:49 +00:00
|
|
|
import initNext, * as next from './'
|
2017-10-15 21:19:01 +00:00
|
|
|
import ErrorDebugComponent from '../lib/error-debug'
|
2017-04-18 04:18:43 +00:00
|
|
|
import initOnDemandEntries from './on-demand-entries-client'
|
|
|
|
import initWebpackHMR from './webpack-hot-middleware-client'
|
2017-01-06 15:23:54 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
require('@zeit/source-map-support/browser-source-map-support')
|
|
|
|
|
2017-10-14 17:20:49 +00:00
|
|
|
window.next = next
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2017-10-21 10:58:59 +00:00
|
|
|
initNext({ ErrorDebugComponent, stripAnsi })
|
2017-04-18 04:18:43 +00:00
|
|
|
.then((emitter) => {
|
|
|
|
initOnDemandEntries()
|
|
|
|
initWebpackHMR()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2017-10-21 10:58:59 +00:00
|
|
|
console.error(stripAnsi(`${err.message}\n${err.stack}`))
|
2017-04-18 04:18:43 +00:00
|
|
|
})
|