Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b567e12

Browse files
jorgemanrubiagobijan
andcommittedDec 26, 2024·
Don't choke when controller is not present in the request
Co-authored-by: Bijan Rahnema <bijan@screenisland.com>
1 parent 74c3329 commit b567e12

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed
 

‎lib/hotwire/spark/middleware.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ def initialize(app)
66
def call(env)
77
status, headers, response = @app.call(env)
88

9-
if html_response?(headers)
10-
@request = ActionDispatch::Request.new(env)
9+
@request = ActionDispatch::Request.new(env)
10+
11+
if interceptable_request? && html_response?(headers)
1112
html = html_from(response)
1213
html = inject_javascript(html)
1314
html = inject_options(html)
@@ -19,6 +20,10 @@ def call(env)
1920
end
2021

2122
private
23+
def interceptable_request?
24+
@request.controller_instance.present?
25+
end
26+
2227
def html_response?(headers)
2328
headers["Content-Type"]&.include?("text/html")
2429
end

‎test/dummy/config/routes.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Rails.application.routes.draw do
22
root to: "home#show"
3+
4+
get "/redirected", to: redirect("/")
35
end

‎test/middleware_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "application_system_test_case"
2+
3+
class MiddlewareTest < ActionDispatch::IntegrationTest
4+
test "error" do
5+
get "/redirected"
6+
end
7+
end

0 commit comments

Comments
 (0)
Please sign in to comment.