Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser Back button is not working for some feature in stimulus js

I have implemented stimulus js in rails hotwire. For other features, the browser back button is working fine, unfortunately, for the product detail page, whenever I use the browser back button, it stays on one page. After trying 2/3 times, it shows the URL of the previous page. But it does not take me to the previous page until I reload.

I have already shared a short video and code on GitHub as a stimulus.js issue. Url is added below:

  • No custom routing has been used at the frontend level.
  • Turbo has been used on the page.

https://github.com/hotwired/stimulus/issues/589

like image 202
RABEYA KHATUN MUNA Avatar asked Jan 30 '26 17:01

RABEYA KHATUN MUNA


1 Answers

On all non-stimulus pages that you want to be able to go Back to a stimulus-oriented page, add this script. It causes a reload only when going back to Turbo-based pages:

<script>
  if (window.history.state && window.history.state.turbo) {
    window.addEventListener("popstate", function () { location.reload(true); });
  }
</script>

(Had this issue surface in a gem I maintain called The Brick which auto-builds views in RAM. Worked everywhere except for stimulus sites, and finally figured out this patch!)

like image 62
Lorin Thwaits Avatar answered Feb 01 '26 07:02

Lorin Thwaits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!