hotwired / stimulus

A modest JavaScript framework for the HTML you already have
https://stimulus.hotwired.dev/
MIT License
12.71k stars 424 forks source link

Turbo Drive: Stimulus doesn't disconnect/connect after redirection to same URL #775

Open elsurudo opened 4 months ago

elsurudo commented 4 months ago

Hello,

Basically as the title says: after a Turbo form submission, if I redirect to the same page then the Stimulus lifecycle events don't fire. Any new Stimulus controllers that are in the redirected-to page don't have their connect methods called, and hence they don't initialize.

Is it a bug, or expected behaviour? And is there a workaround?

tpaulshippy commented 4 months ago

Reminds me of https://github.com/hotwired/turbo/issues/1083#issuecomment-2145264451

Is morphing enabled?

elsurudo commented 4 months ago

Morphing is indeed enabled, and it does seem related to that thread.

The solutions in that thread didn't work for me, although I was able to get around the issue by adding a "dummy" param (for example success=true) to the path I redirect to to force the disconnect/reconnect. It's a bit ugly, but at least works for now.

Still, this was pretty surprising behaviour, so it would be nice to have a better, "sanctioned" solution to this.

elsurudo commented 4 months ago

This also impacts the following (quite common I imagine?) use-case:

  1. Submit form (using Turbo) that happens to have a Stimulus controller on it
  2. There is a form error, hence we render 422 unprocessable entity
  3. Errors are shown, but Stimulus controllers on the form are not loaded (form is broken)