diff --git a/lib/hotwire/spark/middleware.rb b/lib/hotwire/spark/middleware.rb index 1ff1bc1..5e31d4e 100644 --- a/lib/hotwire/spark/middleware.rb +++ b/lib/hotwire/spark/middleware.rb @@ -6,8 +6,9 @@ def initialize(app) def call(env) status, headers, response = @app.call(env) - if html_response?(headers) - @request = ActionDispatch::Request.new(env) + @request = ActionDispatch::Request.new(env) + + if interceptable_request? && html_response?(headers) html = html_from(response) html = inject_javascript(html) html = inject_options(html) @@ -19,6 +20,10 @@ def call(env) end private + def interceptable_request? + @request.controller_instance.present? + end + def html_response?(headers) headers["Content-Type"]&.include?("text/html") end diff --git a/test/dummy/config/routes.rb b/test/dummy/config/routes.rb index 0c2247f..a23839b 100644 --- a/test/dummy/config/routes.rb +++ b/test/dummy/config/routes.rb @@ -1,3 +1,5 @@ Rails.application.routes.draw do root to: "home#show" + + get "/redirected", to: redirect("/") end diff --git a/test/middleware_test.rb b/test/middleware_test.rb new file mode 100644 index 0000000..e61d019 --- /dev/null +++ b/test/middleware_test.rb @@ -0,0 +1,7 @@ +require "application_system_test_case" + +class MiddlewareTest < ActionDispatch::IntegrationTest + test "error" do + get "/redirected" + end +end