stimulusreflex / stimulus_reflex

Build reactive applications with the Rails tooling you already know and love.
https://docs.stimulusreflex.com
MIT License
2.27k stars 171 forks source link

Using middleware causes Rails actions to be invoked twice #564

Closed shepmaster closed 1 year ago

shepmaster commented 2 years ago

Bug Report

Describe the bug

When a middleware is used, Rails actions are invoked twice for every Reflex action.

To Reproduce

Add a middleware:

class DummyMiddleware
  def initialize(app)
    @app = app
  end

  def call(env)
    @app.call(env)
  end
end

StimulusReflex.configure do |config|
  config.middleware.use DummyMiddleware
end

Then perform a page morph. In our application, we use Stimulus Reflex for live form validation, so every (debounced) input triggers a reflex. In the logs below, I'm hitting a in a form field.

Expected behavior

The controller action is invoked once:

StimulusReflex::Channel#receive({ ... })

Processing by ProjectsController#new as HTML
Completed 200 OK in 109ms (Views: 69.5ms | ActiveRecord: 18.6ms | Allocations: 108076)

[ActionCable] Broadcasting to StimulusReflex::Channel:1;1: { ... }
StimulusReflex::Channel transmitting ... (via streamed from StimulusReflex::Channel:1;1)

[46cc5e92] 1/1 StimulusReflex::Reflex#default_reflex -> #new_project via Page Morph (morph)

Actual behavior

The controller action is invoked twice:

StimulusReflex::Channel#receive({ ... })

Processing by ProjectsController#new as HTML
Completed 200 OK in 118ms (Views: 71.5ms | ActiveRecord: 17.2ms | Allocations: 109144)

Processing by ProjectsController#new as HTML
Completed 200 OK in 6ms (Views: 5.3ms | ActiveRecord: 0.0ms | Allocations: 9249)

[ActionCable] Broadcasting to StimulusReflex::Channel:1;1: { ... }

[46cc5e92] 1/1 StimulusReflex::Reflex#default_reflex -> #new_project via Page Morph (morph)

StimulusReflex::Channel transmitting ... (via streamed from StimulusReflex::Channel:1;1)

Versions

StimulusReflex

External tools

Browser

leastbad commented 2 years ago

Jake, can you comment on whether this issue persists after implementing the solution discussed in #565, please?

marcoroth commented 1 year ago

I'm going to close this based on the provided solution in #565. Please feel free to re-open this issue if you still encounter something weird, thank you!