Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't choke when controller is not present in the request #55

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/hotwire/spark/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rails.application.routes.draw do
root to: "home#show"

get "/redirected", to: redirect("/")
end
7 changes: 7 additions & 0 deletions test/middleware_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "application_system_test_case"

class MiddlewareTest < ActionDispatch::IntegrationTest
test "error" do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test renamed in 7ded7ac

get "/redirected"
end
end
Loading