Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Next.js link doesn't render with page scrolled at the top

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?

like image 546
Aessandro Avatar asked Sep 19 '19 09:09

Aessandro


People also ask

Why are pages not scrolled to the top upon navigation?

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.

Why does nextjs slow down my website when I import HTML?

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.

How to fix nextjs has some bug when importing global HTML styles?

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.

Is there a scroll reset option in componentdidmount?

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


4 Answers

Ended up doing this in the main app.js file:

  componentDidMount() {
    Router.events.on('routeChangeComplete', () => {
      window.scroll({
        top: 0,
        left: 0,
        behavior: 'smooth'
      });
    });
  }
like image 102
Aessandro Avatar answered Nov 11 '22 17:11

Aessandro


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.

like image 32
Divine Hycenth Avatar answered Nov 11 '22 18:11

Divine Hycenth


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.

like image 39
Noob Avatar answered Nov 11 '22 19:11

Noob


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

like image 28
Miguel T Avatar answered Nov 11 '22 17:11

Miguel T