Skip to content

Instantly share code, notes, and snippets.

@wickywills
Last active August 30, 2019 08:56
Show Gist options
  • Save wickywills/88082fb2cab48fa138f75b7cf1a42a2b to your computer and use it in GitHub Desktop.
Save wickywills/88082fb2cab48fa138f75b7cf1a42a2b to your computer and use it in GitHub Desktop.
Useful for mini carts not updating when back button is pressed (bfCache), mainly in Safari but also Firefox. Triggers a page reload.
Once your page is in a final state that you don't want to outlive a redirect, you can just bind the pageshow event and then check for the "persisted" property being true to determine if you should reload it.
```
window.addEventListener("pageshow", function(evt){
if(evt.persisted){
setTimeout(function(){
window.location.reload();
},10);
}
}, false);
```
I was having problems with the count on a mini-cart, so in addition to the above I had to keep the count element in the DOM, but instead use a class of "hide" to hide/show it depending on how many elements were in the cart.
Another solution to disable bfCache is to use the following:
```
window.addEventListener('unload', function () {});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment