2017-04-18 04:18:43 +00:00
|
|
|
import 'react-hot-loader/patch'
|
2017-04-01 21:03:40 +00:00
|
|
|
import ReactReconciler from 'react-dom/lib/ReactReconciler'
|
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
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
const next = window.next = require('./')
|
|
|
|
|
2017-04-18 04:18:43 +00:00
|
|
|
next.default()
|
|
|
|
.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) => {
|
|
|
|
console.error(`${err.message}\n${err.stack}`)
|
|
|
|
})
|
2017-04-01 21:03:40 +00:00
|
|
|
|
|
|
|
// This is a patch to catch most of the errors throw inside React components.
|
|
|
|
const originalMountComponent = ReactReconciler.mountComponent
|
|
|
|
ReactReconciler.mountComponent = function (...args) {
|
|
|
|
try {
|
|
|
|
return originalMountComponent(...args)
|
|
|
|
} catch (err) {
|
|
|
|
next.renderError(err)
|
|
|
|
err.abort = true
|
|
|
|
throw err
|
|
|
|
}
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|