2017-01-05 17:27:39 +00:00
|
|
|
import patch from './patch-react'
|
2017-01-31 03:26:17 +00:00
|
|
|
import evalScript from '../lib/eval-script'
|
|
|
|
|
|
|
|
const { __NEXT_DATA__: { errorComponent } } = window
|
|
|
|
const ErrorComponent = evalScript(errorComponent).default
|
2016-10-14 15:05:08 +00:00
|
|
|
|
2017-01-05 17:27:39 +00:00
|
|
|
// apply patch first
|
|
|
|
patch((err) => {
|
|
|
|
console.error(err)
|
2017-01-12 01:58:20 +00:00
|
|
|
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
onError(err)
|
|
|
|
})
|
2017-01-05 17:27:39 +00:00
|
|
|
})
|
|
|
|
|
2017-01-06 15:23:54 +00:00
|
|
|
require('react-hot-loader/patch')
|
|
|
|
|
2017-01-12 01:58:20 +00:00
|
|
|
const next = window.next = require('./')
|
|
|
|
|
2017-01-31 03:26:17 +00:00
|
|
|
const emitter = next.default(onError)
|
2017-01-12 01:58:20 +00:00
|
|
|
|
|
|
|
function onError (err) {
|
|
|
|
// just show the debug screen but don't render ErrorComponent
|
|
|
|
// so that the current component doesn't lose props
|
2017-01-31 03:26:17 +00:00
|
|
|
next.render({ err, emitter })
|
2017-01-12 01:58:20 +00:00
|
|
|
}
|
2017-01-31 03:26:17 +00:00
|
|
|
|
|
|
|
let lastScroll
|
|
|
|
|
|
|
|
emitter.on('before-reactdom-render', ({ Component }) => {
|
|
|
|
// 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 }) => {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|