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:
https://github.com/hotwired/stimulus/issues/589
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!)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With