Scroll restoration happens too early before the page gets rendered after hitting browser back button
  • 10-May-2023
Lightrun Team
Author Lightrun Team
Share
Scroll restoration happens too early before the page gets rendered after hitting browser back button

Scroll restoration happens too early before the page gets rendered after hitting browser back button

Lightrun Team
Lightrun Team
10-May-2023

Explanation of the problem

Upon investigating the issues within the repository, it appears that the problem at hand is not a duplicate. The issue arises when transitioning from one page to another using the next <Link /> component, and subsequently clicking the browser’s back button. If the previous page contains the getInitialProps method, which has a certain processing time, the scroll position of the previous page is restored before the rendering of the page completes. As a result, the expected behavior is for the content of the previous page, specifically the “go back” text, to remain visible without scrolling back to the previous position until the previous page finishes rendering.

To address this issue, a demonstration has been provided, and the source code can be accessed through the provided link. The desired outcome is that after clicking the back button, the “go back” text should remain visible without scrolling to the previous position until the rendering of the previous page is complete.

To achieve this, it is necessary to implement a solution that manages the scroll position during the navigation process. This can involve utilizing the next/router module and its events, such as routeChangeStart and routeChangeComplete, to track the transition and manage the scroll position accordingly. By capturing the current scroll position before the route change and ensuring it is maintained until the previous page finishes rendering, the desired behavior can be achieved. The specific implementation details can be found in the provided source code.

 

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for Scroll restoration happens too early before the page gets rendered after hitting browser back button

Overall, the provided answers offer insights into the issue of scroll position restoration when using Next.js and navigating back in the browser. Answer 1 acknowledges the problem and provides a link to a forum thread where similar issues are discussed. It explains that the browser’s default scrolling behavior, coupled with the asynchronous loading of data through getInitialProps(), can result in incorrect scroll positions when navigating back. The suggested solutions involve caching the data locally and rendering the page from that cache, or manually controlling scrolling using Router.push(). However, it is mentioned that Next.js does not currently provide built-in scroll management functionality.

Answer 2 presents an implementation solution for restoring scroll position when the browser’s back button is clicked. By using the lifecycle method componentDidMount() in the _app.js file, the scroll position is tracked and cached before a route change occurs. Then, when navigating back, the cached page height is used to restore the scroll position by adjusting the HTML element’s height. This approach provides a practical way to address the scroll restoration issue in Next.js.

In summary, both answers recognize the problem of scroll position restoration when navigating back in Next.js. Answer 1 suggests caching the data locally or controlling scrolling manually as potential solutions, while also mentioning the absence of built-in scroll management in Next.js. Answer 2 offers a specific implementation using componentDidMount() in the _app.js file to track and restore the scroll position.

Other popular problems with Vercel Next.js

Problem: Build errors when deploying to Vercel

Some users have reported encountering build errors when attempting to deploy their Next.js application to Vercel. These errors can occur due to a variety of reasons, such as missing dependencies or configuration issues.

Solution:

To fix build errors when deploying to Vercel, it is important to check the error logs and messages provided by Vercel and understand the root cause of the issue. This can be done by reviewing the logs in the Vercel Dashboard or by running the build locally and comparing the results. Common causes of build errors include missing dependencies, incorrect versions of dependencies, and configuration issues. It’s important to ensure that the necessary dependencies are installed and that the versions are compatible with the version of Next.js that is being used. Additionally, checking the application’s package.json and next.config.js files can help identify any configuration errors.

Problem: Routing issues with dynamic routes

Dynamic routing is a feature of Next.js that allows for the creation of routes that are based on dynamic data. Some users have reported issues with dynamic routing, such as 404 errors or incorrect routing when using the getStaticPaths and getStaticProps functions.

Solution:

To resolve routing issues with dynamic routes in Next.js, it is important to ensure that the getStaticPaths and getStaticProps functions are properly configured. The getStaticPaths function should return an array of paths that should be pre-rendered, and the getStaticProps function should return an object that contains the data needed for the component to render. Additionally, it’s important to ensure that the paths returned by the getStaticPaths function match the paths used in the application’s Link components and Router.push calls.

Problem: Server-side rendering issues

Next.js applications are built with server-side rendering (SSR) in mind, but some users have reported issues with SSR, such as slow loading times or incorrect rendering of components.

Solution:

To resolve server-side rendering issues in Next.js, it is important to ensure that the application is properly optimized for SSR. This can be done by reducing the amount of data that is loaded during the initial render, using efficient data structures, and caching data on the server. Additionally, using the getStaticProps function can improve the performance of the application by pre-rendering pages on the server. It’s also important to make sure that the code uses the Next.js’s lifecycle methods like getInitialProps and getServerSideProps properly.

A brief introduction to Vercel Next.js

Vercel Next.js is a framework for building server-rendered React applications. It is built on top of Node.js and provides a set of features that are designed to simplify the process of building, deploying, and managing React applications. Next.js provides a powerful development environment that allows developers to build applications with a highly performant and scalable architecture.

One of the key features of Vercel Next.js is its support for server-side rendering (SSR), which allows for the rendering of React components on the server. This can result in improved performance and SEO benefits for the application. Additionally, Next.js includes a powerful development workflow that includes features such as automatic code splitting, hot-module replacement, and a development server that supports both client-side and server-side rendering. It also has a built-in support for dynamic routing, which allows developers to create routes that are based on dynamic data, and provides a set of APIs that allow developers to interact with the server-side rendering pipeline.

Most popular use cases for Vercel Next.js

  1. Building server-rendered React applications: Vercel Next.js is a framework for building server-rendered React applications. This allows developers to build web applications that have fast loading times, improved SEO, and a high level of performance. It also provides a development environment that is designed to simplify the process of building, deploying, and managing React applications.
  2. Dynamic routing: Next.js provides support for dynamic routing, which allows developers to create routes that are based on dynamic data. This can be achieved using the getStaticPaths and getStaticProps functions. The getStaticPaths function returns an array of paths that should be pre-rendered, and the getStaticProps function returns an object that contains the data needed for the component to render. The following code block illustrates an example of dynamic routing:
import { useRouter } from "next/router";

export async function getStaticPaths() {
  // Return a list of possible value for id
}

export async function getStaticProps({ params }) {
  // Fetch necessary data for the blog post using params.id
}

export default function Post({ postData }) {
  const router = useRouter();
  const { id } = router.query;

  return <h1>Post: {id}</h1>;
}
  1. Server-side rendering (SSR): Vercel Next.js makes it easy to build server-rendered React applications. This allows developers to pre-render pages on the server and improve the performance of the application. Additionally, it provides a set of APIs that allow developers to interact with the server-side rendering pipeline. This can also result in improved SEO and fast loading times for the application.
Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.