1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/client/next-dev.js
2017-07-02 11:59:19 +05:30

52 lines
1.5 KiB
JavaScript

import 'react-hot-loader/patch'
import ReactReconciler from 'react-dom/lib/ReactReconciler'
import initOnDemandEntries from './on-demand-entries-client'
import initWebpackHMR from './webpack-hot-middleware-client'
const next = window.next = require('./')
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}`)
})
// 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) {
if (!err.abort) {
next.renderError(err)
err.abort = true
}
throw err
}
}