Skip to content

Commit 66039c9

Browse files
committed
Use x_user_agent cookie for platform detection in Hotwire Native
Turbo's offline/service worker sets an x_user_agent cookie with the original browser user agent. This ensures platform detection works correctly for Hotwire Native apps where service worker requests may not carry the overridden user agent header.
1 parent 8c2318e commit 66039c9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

app/controllers/concerns/set_platform.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ module SetPlatform
77

88
private
99
def platform
10-
@platform ||= ApplicationPlatform.new(request.user_agent)
10+
@platform ||= ApplicationPlatform.new(cookies[:x_user_agent].presence || request.user_agent)
1111
end
1212
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "test_helper"
2+
3+
class SetPlatformTest < ActionDispatch::IntegrationTest
4+
DESKTOP_UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
5+
NATIVE_IOS_UA = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Hotwire Native iOS/1.0"
6+
7+
test "uses the request user agent by default" do
8+
sign_in_as :david
9+
10+
get board_path(boards(:writebook)), headers: { "User-Agent" => DESKTOP_UA }
11+
assert_match 'data-platform="desktop web"', response.body
12+
end
13+
14+
test "prefers x_user_agent cookie over request user agent" do
15+
sign_in_as :david
16+
17+
cookies[:x_user_agent] = NATIVE_IOS_UA
18+
get board_path(boards(:writebook)), headers: { "User-Agent" => DESKTOP_UA }
19+
assert_match 'data-platform="native ios"', response.body
20+
end
21+
end

0 commit comments

Comments
 (0)