2

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

  • I recommend you take a bit more time to update this question with the minimum viable code that reproduces the problem. The code should ideally be put into the question itself without relying on an external link. It will be very hard for anyone to give you an answer otherwise. It might also be good to explain if this code also uses turbo or not, or clarify if there is any kind of custom routing code in place. It seems unlikely that Stimulus on its own would impact the browser back behaviour. – LB Ben Johnston Oct 08 '22 at 00:58
  • There is no cutom routing and turbo has been used in this code. We added the turbo in the application.js – RABEYA KHATUN MUNA Oct 09 '22 at 18:12

1 Answers1

0

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!)

Lorin Thwaits
  • 301
  • 1
  • 3