Repository navigation
Fix: Scroll restoration and pages states on PLP when back from PDP - #2054
Conversation
- using session storage to save/retrieve opened pages (state)
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 90df651:
|
icazevedo
left a comment
There was a problem hiding this comment.
I'm so happy we actually did this!
eduardoformiga
left a comment
There was a problem hiding this comment.
Niceee! in my tests it works as expected ✨
Co-authored-by: Ícaro Azevedo <[email protected]>
| const newState = [...state, next] | ||
| setPagesSessionStorage(newState) | ||
| return newState |
There was a problem hiding this comment.
if !window.history.state?.key is false, should we still return newState?
There was a problem hiding this comment.
Yes! The saving mechanism is only to fix the issue when returning to that PLP from another page. If it can be saved, you should still be able to open new gallery pages (that's what returning newState does).
icazevedo
left a comment
There was a problem hiding this comment.
I agree with @lucasfp13 suggestions!
| const newState = [...state, next] | ||
| setPagesSessionStorage(newState) | ||
| return newState |
There was a problem hiding this comment.
Yes! The saving mechanism is only to fix the issue when returning to that PLP from another page. If it can be saved, you should still be able to open new gallery pages (that's what returning newState does).
Co-authored-by: Lucas Feijó <[email protected]>
Co-authored-by: Lucas Feijó <[email protected]>
## What's the purpose of this pull request? From a current Next.js's known issue ([discussion#44013](vercel/next.js#44013)), these changes aim to add an experimental custom hook to handle scroll restoration when navigating back from PDPs to PLPs. https://github.com/user-attachments/assets/9a452081-4c65-4a83-8c16-9775f97d226d Along with this experimental hook, some adjustments needed to be made to correct the behavior of related components, such as `Sentinel`. While the Next.js experimental `scrollRestoration` flag is not working as it should, we are proposing this FastStore's experimental scroll restoration alternative. ## How it works? While navigating through PLPs and loading more pages, when users navigate to a PDP and navigate back the scroll should be restored to the last position before the navigation. ### Starters Deploy Preview vtex-sites/faststoreqa.store#878 ## References #2054
What's the purpose of this pull request?
reported issue: #1972
Scenario: On PLP, click on
Load more products, select a product, go to product's PLP and go back to PLP using browser's back button.The default behavior of the Next.js App Router is to scroll to the top of a new route. We enabled the
scrollRestorationexperimental feature. That is a default behavior in Next.js 13.How it works?
After studying different approach, we decided to change the previous behaviour.
Previously, given the following scenario:
Load more products-> Select a product's -> go to product's PLP -> back to PLPWe were displaying only the last viewed 'page' (in this example, 'page=1') with the 'Previous Page' button available, which was causing a mismatch in the scroll position.
Looking for a better UX experience and keeping the scroll position as it should, we're now retrieving the previous state with the once-opened pages. To do so, we are storing the state in the session storage and retrieve it from it when needed.
To map it, we are using the
__fs_gallery_pageprefix along with a unique key - this key is a property from the window.history.state , which allow us to uniquely identifying different history entries.This approach is inspired on how the nextjs scrollRestoration is implemented.
How to test it?
Using this preview link: https://starter-git-fix-plp-pdp-back-fs-1285-2-faststore.vercel.app/
Load more productsThe previously opened pages and scroll position should be restored.
Starters Deploy Preview
WIP - https://github.com/vtex-sites/starter.store/pull/217
References
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
https://github.com/vercel/next.js/blob/a44b4f85b56ef1c5cfe9ab931839a24241c10230/packages/next/src/shared/lib/router/router.ts#L950
https://levelup.gitconnected.com/browser-navigation-a-guide-to-window-location-and-window-history-in-javascript-45284c32dabd
Thanks, @icazevedo , for delving into this matter with me and exploring various approaches to address this issue 🌟