next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Scroll position not saved when using back button to go to page using getServerSideProps.

Open Muljayan opened this issue 4 years ago • 3 comments
trafficstars

What version of Next.js are you using?

10.2.3

What version of Node.js are you using?

12.6.3

What browser are you using?

Chrome, Firefox

What operating system are you using?

Ubuntu 18.04

How are you deploying your application?

next start, next dev

Describe the Bug

This bug was addressed for previous versions of Next prior to version 10. A few of them are as follows #9989 ,#12530. This issue seems to have come back.

  • Assume there are 99 posts
  • Scroll down to the 50th post and click on it.
  • 50th post opens up
  • Press the back button
  • Expectation is to return to the previous position (50th post)
  • However due to the bug the scroll position returns to the top, sometimes it goes to weird locations between the 1st and the 50th post.

This issue occurs when we return to a page which uses getServerSideProps with the back button. I tried it with getInitialProps and it works fine.

Expected Behavior

The expected behavior is to go back to the previous scroll position after clicking the back button. This does not happen.

To Reproduce

index.js in the pages folder


import React from 'react'
import Link from 'next/link';

const Home = ({ posts }) => {

  const projectList = posts.map((postId) => (
    <li key={postId}>
      <Link href={`/posts/${postId}`}>
        <a className="hover:bg-light-blue-500 hover:border-transparent hover:shadow-lg group block rounded-lg p-4 border border-gray-200">
          <img src="https://via.placeholder.com/600x400.png" alt="" />
          <div className="leading-6 font-medium text-black">
            Project ID : {postId}
          </div>
        </a>
      </Link>
    </li>
  ))

  return (
    <section className="px-4 sm:px-6 lg:px-4 xl:px-6 pt-4 pb-4 sm:pb-6 lg:pb-4 xl:pb-6 space-y-4">
      <ul className="grid grid-cols-1 gap-4">
        {projectList}
      </ul>
    </section>
  )
}

export const getServerSideProps = async () => {
  const posts = [];
  for (let index = 0; index < 1000; index++) {
    posts.push(index);
  }
  setTimeout(() => {
  }, 10000);
  return {
    props: {
      posts
    }
  };
};

// Using this instead of getServerSideProps works as intended
// Home.getInitialProps = async (ctx) => {
//   const posts = [];
//   for (let index = 0; index < 1000; index++) {
//     posts.push(index);
//   }
//   setTimeout(() => {
//   }, 10000);
//   return { posts };
// }

export default Home


[id.js] file in the pages/posts folder


export const getServerSideProps = async (context) => {
  const { query } = context;
  const id = query.id;
  return {
    props: {
      id,
    },
  };
};

const Posts = ({id}) => {
  return (
    <h1>
      {id}
    </h1>
  )
}

export default Posts

Muljayan avatar Jun 12 '21 04:06 Muljayan

Experiencing the same issue. would be nice to have a fix itself or suggestion of a possible workaround.

JayeUCSC avatar Jun 13 '21 05:06 JayeUCSC

any fix yet?

this is a big issue

1finedev avatar Jul 07 '21 18:07 1finedev

Does setting the scrollRestoration experimental option make any difference?

module.exports = {
  experimental: {
    scrollRestoration: true,
  },
};

smaeda-ks avatar Nov 16 '22 05:11 smaeda-ks

Still buggy especially with fragment links (# anchors). Current state, see https://github.com/vercel/next.js/issues/37893#issuecomment-1382858670

don-esteban avatar Jan 14 '23 17:01 don-esteban

The problem is still present in NextJs 13.4.12 appDir.

Update: On IOS Chrome we cannot reproduce the issue. Only IOS Safari is reproducible.

l-you avatar Aug 02 '23 17:08 l-you

I've reproduced the issue on almost all of my PWA apps on Chrome. Close to 20 of them. I cannot however reproduce this on Safari as of yet.

guylepage3 avatar Mar 15 '24 14:03 guylepage3

See: https://github.com/vercel/next.js/issues/37893#issuecomment-1619916412. I have a Next.js 14 App Router web app running without issues on any of the major browsers.

don-esteban avatar Mar 16 '24 00:03 don-esteban