Shopify / Shopify/liquid

JavaScript functions do not rerun when returning to page due to bfcache in Chrome

Open
#2,009 3 comments 0 reactions 1 assignee View on GitHub

@karreiro is already working on this.

Since Dec 4, 2025.

Dominant language
Ruby
Stars
11.9k
Forks
1.5k
Avg merge
17h 55m
Merged PRs (30d)
3

Description

Describe the current behavior

JavaScript functions intended to run once per page load are cached by the browser. When you navigate away from the page and then come back, the function does not run again. Instead, it retains only the last computed value or state from the previous run.

<div class="animate">Hello World</div>
{% javascript %}
document.addEventListener('DOMContentLoaded', function() {
  const element = document.querySelector('.animate');
  element.classList.add('move'); 
});
{% endjavascript %}
{% stylesheet %}
.animate {
  display: inline-block;
  transition: transform 0.3s ease-in-out; 
}
.animate.move {
  transform: translateX(300px);
}
{% endstylesheet %}

When navigating away from the page and then returning, the function does not run again, and only the last state from the previous run is shown.

Expected behavior

The function should rerun whenever the user returns to the page, rather than relying on workarounds such as:

window.addEventListener("pageshow", (event) => {
    if (event.persisted) {
        myFunction();
    } else {
        myFunction();
    }
});

Screenshots / Video

Image

Environment
  • Browser(s): Google Chrome Version 142.0.7444.162
  • Device: Desktop
  • OS: macOS 15.6.1
Additional context

We have seen similar problems, for instance in the Dawn theme, where users have been using hacks to update their cart. Why does Shopify have a hard time with bfcache? We don’t see this problem in other frameworks.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.