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
Tim Neutkens bd83ed5c54 [WIP] Remember scroll position on error (#911)
* Remember scroll position on error

* Added comment + check if lastScroll was set

* Remove check for lastAppProps

* Use events to make scroll persistence dev-only

* Return EventEmitter from next()

* Update next-dev.js
2017-01-31 08:56:17 +05:30

49 lines
1.2 KiB
JavaScript

import patch from './patch-react'
import evalScript from '../lib/eval-script'
const { __NEXT_DATA__: { errorComponent } } = window
const ErrorComponent = evalScript(errorComponent).default
// apply patch first
patch((err) => {
console.error(err)
Promise.resolve().then(() => {
onError(err)
})
})
require('react-hot-loader/patch')
const next = window.next = require('./')
const emitter = next.default(onError)
function onError (err) {
// just show the debug screen but don't render ErrorComponent
// so that the current component doesn't lose props
next.render({ err, emitter })
}
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
}
})