Skip to content

When navigating back to a keep-alive page, the scroll position is not being correctly restored to its previous state. #10

Description

@Dandelion-drq

Next.js version: 13.5.6
node version: 18.x +


I've encountered an issue while using this library in my Next.js 13 pages router project. When I navigate back to a keep-alive page, the page elements do not re-render, but the scroll position does not restore to the original position.

The issue is demonstrated in the image below:

1.mp4

Then, I compared the code in this gist and noticed a difference: this library restores the scroll position on routeChangeComplete, whereas in the gist, it's done inside useEffect. So, I tried changing routeChangeComplete to useEffect, and it indeed solved the problem.

The modified behavior is shown in the image below:

2.mp4

I tried add some log for debugging, as shown in the code below:

This is the code:
Image

This is the log output:
Image

I found that the routeChangeComplete event is triggered after the page navigation is completed, but at this point, the name still refers to the previous keep-alive page's name. For example, when navigating from / to /page2, the URL printed is /page2, but the name is Home. Then, when navigating back from /page2 to /, the URL printed is /, but the name is undefined (because /page2 is not a keep-alive page). Could this be the reason why the scrollPos is being incorrectly retrieved?

I've created a code repository that reproduces the issue. You can take a look.

Looking forward to your reply, thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions