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 './'
|
2018-04-18 16:18:06 +00:00
|
|
|
import {ClientDebug} 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'
|
2018-04-18 16:18:06 +00:00
|
|
|
import {AppContainer as HotAppContainer} from 'react-hot-loader'
|
|
|
|
import {applySourcemaps} from './source-map-support'
|
2018-01-30 15:40:52 +00:00
|
|
|
|
2017-10-14 17:20:49 +00:00
|
|
|
window.next = next
|
2017-01-12 01:58:20 +00:00
|
|
|
|
2018-04-18 16:18:06 +00:00
|
|
|
initNext({ HotAppContainer, ErrorDebugComponent: ClientDebug, applySourcemaps, 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
|
|
|
})
|