Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Cart UI not updates when using browsers navigation buttons #2166

Open
john-mantas opened this issue Dec 9, 2022 · 5 comments
Open

Comments

@john-mantas
Copy link

Current behavior
If I click the "Add to cart" from the product page the product gets added to cart and the notification bubble also changes to reflect the cart count, but when i click the "back" button from the browser to return to the previous page, the cart bubble shows the previous count (if any). The same thing happens when using the drawer, the products gets added and the drawer opens with the correct content, but when i click the back button the drawer appears to be empty.

Expected behavior
UI should reflect the actual cart content when using the browser history navigation buttons.

Version information

  • Dawn Version: 7.0.1 and 6.0.2
  • Chrome Version: 107.0.5304.121 (Official Build) (arm64)
  • Firefox Version: 107.0.1 (64-bit)
  • Safari Version: 15.6 (17613.3.9.1.5)
  • macOS Version: 12.5 Monterey

Additional context/screenshots

  • This is happening in the preview of the demo store, in the preview of other themes like "Refresh" and also to an older version of dawn that i have on a store
  • The same behavior happens also at firefox and safari
  • The actual cart is updated correctly, that means that the /cart.js always have the correct cart content
  • On page refresh, the cart shows the correct content
  • If i have "Disable cache" enabled in the chrome dev tools the cart seems to work properly

It seems that even the "liquid parts" are not correct, since the is-empty class at <cart-drawer> is handled with liquid and despite that the cart is not empty, this class still gets printed in the html on back/forward navigation.

dawn--cart

dawn--cart-forw

refresh-drawer-o


@john-mantas john-mantas reopened this Jan 26, 2023
@john-mantas john-mantas changed the title Cart UI not updates when using browsers navigation buttons [BUG] Cart UI not updates when using browsers navigation buttons Jan 26, 2023
@tanoargie
Copy link

Hello!
Is there an estimated time to fix this issue?

@facundoperezh
Copy link

Any updates on this issue?

@naxorn
Copy link

naxorn commented May 10, 2023

The underlying cause of this is the browser's use of bfcache. When you use back/forward the entire page's state is loaded from memory without a network request. In JavaScript, you can listen for when the page is restored from cache. Ideally, you would re-render the appropriate sections as needed. However, as a quick hack, for the cart page at least, you could force a page reload in the event listener... so maybe something like:

window.addEventListener('pageshow', (event) => {
  if (event.persisted) { // loading from bfcache   
   // NOTE: If you don't want to modify the theme, you could create a custom liquid section in the header

   // BEST solution
     // re-render appropriate sections, especially cart-icon-bubble, using section rendering api
     // check out cart-drawer.js, cart.js and cart-notification.js

   // HACK solution - not using cart drawer
   // if non-cart page
     // grab new icon using section rendering api
   // else, it's the cart page
    // location.reload(); // force a full reload always
  } else {
   // Nothing needed here, page should refresh normally
  }
});

Of course, if a user disables JavaScript this won't work. The cart icon will always be out of sync on back. When JavaScript is disabled on the cart page, the following might work (not tested) to disable the cache:

<!-- cart page only -->
<head>
  <!-- stuff -->
  <noscript>
    <meta http-equiv="Cache-Control" content="no-store">
  </noscript>
  <!-- stuff -->
</head>

@luiscalmeida
Copy link

I'm on Dawn 11.0 and same thing happens, any news on this?

@rbarreca
Copy link

We're experiencing the same issue intermittently on Dawn 14. We have a checkout UI extension that can potentially add/edit/remove cart items in one-page checkout. If a user changes their cart in checkout, then clicks the back button on the browser, it will sometimes show a stale cart drawer and bubble until you refresh browser.

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

No branches or pull requests

6 participants