I have a component like so:
const Milestone = props => {
const { path, disabled, index, ...rest } = props;
if (disabled) return <MilestoneCheck disabled />;
return (
<Link href={path} passHref>
<a>
<MilestoneCheck {...rest} />
</a>
</Link>
);
};
when I click on the 'Link' to go to the next page and than click the back button to go back to where I came from, the page doesn't load at the top but from the last scrolled position. Adding a 'scrollTop' method on route change would feel not very efficient, is there a more elegant solution to always having the page loading at the top?
Another reason pages are not scrolled to the top upon navigation is when the following style rule exists: There is a Github issue on this here.
The issue for some unknown reason was that NextJS has some bug when a global html style is imported. I had html class in my global styles.scss import that was loaded into my main wrapper. Once I removed the html from the import the scrolling issues stopped and pages loaded as they would on a static site.
A possible cause could be overflow-x: hidden; on one of the root elements: body, html or #__next. You can try to add min-height: 100% to it or replace it with overflow-x: clip;. I was struggling with this issue for a few days and found the solution. The issue for some unknown reason was that NextJS has some bug when a global html style is imported.
I agree there should be a scrollreset option. If you don't want to do it in componentDidMount you can also do Router.push ('/link').then ( () => window.scrollTo (0, 0));. Sorry, something went wrong. Router.push ('/link').then ( () => window.scrollTo (0, 0)); this is the right way to do it, feel free to add it to the readme @gragland
Ended up doing this in the main app.js file:
componentDidMount() {
Router.events.on('routeChangeComplete', () => {
window.scroll({
top: 0,
left: 0,
behavior: 'smooth'
});
});
}
I had this experience and I tried a lot of ways to get it to work but it didn't. This kind of route change behavior is supposed to happen by default except you're doing something different.
I noticed that after I removed overflow-x: hidden
from my html/body
tag everything started to work normally.
Try not to use this form of styling in your html/body element:
html, body {
- overflow-x: hidden;
}
You can set a wrapper element's (i.e div
) maximum width to 100vw
and overflow-x:hidden
to archive the same thing.
I had the same issue when my page would not render at the top. In my case in my global.css
i had this:
html {
font-size: 62.5%;
scroll-behavior: smooth;
}
After removing scroll-behavior: smooth;
everything started working as expected.
I was struggling with this issue for a few days and found the solution.
The issue for some unknown reason was that NextJS has some bug when a global html style is imported. I had html class in my global styles.scss import that was loaded into my main wrapper. Once I removed the html from the import the scrolling issues stopped and pages loaded as they would on a static site.
Found the solution here https://github.com/zeit/next.js/issues/7594
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With