Skip to content

Commit 5040246

Browse files
authored
Give html: rendering param the same treatment as content: (#362)
broadcast_action_to accepts a content: argument to set the turbo-stream template content directly, bypassing any rendering. Other params, like partial: or locals: are delegated to render. Since the arguments are passed directly to render, you might expect you can also use html: as an argument here. However, there's a catch: when using a queue system to broadcast the action later, the html_safe flag of the html param might be lost in the serialization process. Specifically, the flag remains with queue backends that work in process (like `inline` or `async`) but it's lost in other that serialize the payload to JSON (like `resque` or `sidekiq`). The resulting effect is that the broadcast action works fine in development, but then fails when it's deployed to a production system. This problem seems to have tripped a few people: #284 Treating html: the same as content: solves the issue.
1 parent 43bf84a commit 5040246

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

app/channels/turbo/streams/broadcasts.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def broadcast_prepend_to(*streamables, **opts)
3535

3636
def broadcast_action_to(*streamables, action:, target: nil, targets: nil, **rendering)
3737
broadcast_stream_to(*streamables, content: turbo_stream_action_tag(action, target: target, targets: targets, template:
38-
rendering.delete(:content) || (rendering.any? ? render_format(:html, **rendering) : nil)
38+
rendering.delete(:content) || rendering.delete(:html) || (rendering.any? ? render_format(:html, **rendering) : nil)
3939
))
4040
end
4141

test/streams/streams_channel_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ class Turbo::StreamsChannelTest < ActionCable::Channel::TestCase
8989
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: render(options)) do
9090
Turbo::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ".message", **options
9191
end
92+
93+
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<span>test</span>") do
94+
Turbo::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ".message", content: "<span>test</span>"
95+
end
96+
97+
assert_broadcast_on "stream", turbo_stream_action_tag("prepend", targets: ".message", template: "<span>test</span>") do
98+
Turbo::StreamsChannel.broadcast_action_to "stream", action: "prepend", targets: ".message", html: "<span>test</span>"
99+
end
92100
end
93101

94102
test "broadcasting replace later" do

0 commit comments

Comments
 (0)