Skip to content

Fix: Scroll restoration and pages states on PLP when back from PDP - #2054

Merged
hellofanny merged 10 commits into
mainfrom
fix/plp-pdp-back-fs-1285-2
Oct 9, 2023
Merged

hellofanny merged 10 commits into
mainfrom
fix/plp-pdp-back-fs-1285-2

Conversation

@hellofanny

@hellofanny hellofanny commented Oct 6, 2023 •

Copy link
Copy Markdown
Contributor

What's the purpose of this pull request?

  1. Attempts to fix scroll position when going back from the PDP to PLP

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.

Before - PLP drops user at bottom of the page
plp drops users on the bottom
FIx - PLP saves last state (open pages) and keep previous scroll position
plp saves last state (open pages) and keep previous scroll position
  1. Enables scroll restoration on the application
Before - not using scroll restoration Enable scroll restoration
before restore

The default behavior of the Next.js App Router is to scroll to the top of a new route. We enabled the scrollRestoration experimental feature. That is a default behavior in Next.js 13.

experimental: {
    scrollRestoration: true,
},

How it works?

After studying different approach, we decided to change the previous behaviour.
Previously, given the following scenario:

  • Navigating in PLP -> Click on Load more products -> Select a product's -> go to product's PLP -> back to PLP

We 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_page prefix along with a unique key - this key is a property from the window.history.state , which allow us to uniquely identifying different history entries.

image

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/

  1. Navigate to PLP
  2. Click on Load more products
  3. Select a product's -> go to product's PLP
  4. Use the browser's back button and go back to PLP

The 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 🌟

@vercel

vercel Bot commented Oct 6, 2023 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
faststore-site ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 9, 2023 5:46pm

@codesandbox-ci

codesandbox-ci Bot commented Oct 6, 2023 •

Copy link
Copy Markdown

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:

Sandbox Source
Store UI Typescript Configuration

@hellofanny
hellofanny marked this pull request as ready for review October 6, 2023 20:03
@hellofanny
hellofanny requested a review from a team as a code owner October 6, 2023 20:03
@hellofanny
hellofanny requested review from Gmantiqueira and eduardoformiga and removed request for a team October 6, 2023 20:03
@hellofanny hellofanny self-assigned this Oct 6, 2023

@icazevedo icazevedo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm so happy we actually did this!

Comment thread packages/core/next.config.js Resolved

@eduardoformiga eduardoformiga left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Niceee! in my tests it works as expected ✨

Comment on lines +62 to +64
const newState = [...state, next]
setPagesSessionStorage(newState)
return newState

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if !window.history.state?.key is false, should we still return newState?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@lucasfp13 lucasfp13 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice solution, LGTM!

@icazevedo icazevedo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @lucasfp13 suggestions!

Comment on lines +62 to +64
const newState = [...state, next]
setPagesSessionStorage(newState)
return newState

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@hellofanny
hellofanny merged commit 64e6c01 into main Oct 9, 2023
@hellofanny
hellofanny deleted the fix/plp-pdp-back-fs-1285-2 branch October 9, 2023 17:52
lucasfp13 added a commit that referenced this pull request Oct 30, 2025
## 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

This branch was successfully deployed

1 active deployment
Preview — 90df6514 Deployed Oct 9, 2023 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants