-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Remember scroll position on error #911
Changes from 1 commit
ed81a27
7441832
e77d787
4f8334e
41525fe
d3ba6a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ const { | |
const Component = evalScript(component).default | ||
const ErrorComponent = evalScript(errorComponent).default | ||
let lastAppProps | ||
let lastScroll | ||
|
||
export const router = createRouter(pathname, query, { | ||
Component, | ||
|
@@ -65,10 +66,27 @@ async function doRender ({ Component, props, err }) { | |
props = await loadGetInitialProps(Component, { err, pathname, query }) | ||
} | ||
|
||
if (Component === ErrorComponent) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also add a comment on why we need to keep this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done 👍 |
||
lastScroll = { | ||
x: window.pageXOffset, | ||
y: window.pageYOffset | ||
} | ||
} | ||
|
||
Component = Component || lastAppProps.Component | ||
props = props || lastAppProps.props | ||
|
||
const appProps = { Component, props, err, router, headManager } | ||
lastAppProps = appProps | ||
ReactDOM.render(createElement(App, appProps), container) | ||
|
||
if (lastScroll && | ||
Component !== ErrorComponent && | ||
lastAppProps.Component === ErrorComponent) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to do this check? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It basically makes sure that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I got it. Since we also check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I removed it. Think the addition of |
||
// Restore scroll after ErrorComponent was replaced with a page component by HMR | ||
const {x, y} = lastScroll | ||
window.scroll(x, y) | ||
lastScroll = false | ||
} | ||
|
||
lastAppProps = appProps | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happen if we render the Error component twice. Then it might override the actual
lastScroll
Could you test for that case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, good catch 👍