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

Include "Turbo Native Android" in the user agent substring for compatibility #67

Merged
merged 1 commit into from
Oct 22, 2024

Conversation

jayohms
Copy link
Contributor

@jayohms jayohms commented Oct 22, 2024

This allows servers to match against Hotwire Native Android or Turbo Native Android user agents. This matches the approach in the iOS library.

This is a follow up to #66

@jayohms jayohms merged commit 218a7f7 into main Oct 22, 2024
1 check passed
@jayohms jayohms deleted the user-agent-compatibility branch October 22, 2024 14:42
@donnfelker
Copy link
Contributor

Good call. Making a comment for others in case they run into this ...

I just ran into this issue this morning. Server was looking for Turbo Native Android in the web bridge that I had installed. Therefore the stimulus controller was not connecting.

Other thing that tripped me up was that I had my code to replace the user agent value (RE: #66) set up BEFORE, I registered my BridgeComponents, therefore, I was getting a user agent that looked like this:

Hotwire Native Android; bridge-components: []; Mozilla/5.0 (Linux; Android 14; sdk_gphone64_arm64 Build/UE1A.230829.036.A4; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/129.0.6668.100 Mobile Safari/537.36

Due to the bridge-components array being empty, the isHotwireNativeApp in the Hotwire Bridge Component JS file would return false, therefore the bridge would not connect.

Note for others - When setting up Android Hotwire in your app, the user agent needs to be set after you register the bridge components, like this:

// Register Bridge Components
Hotwire.registerBridgeComponents(
    BridgeComponentFactory("form", ::FormComponent)
)

// Needs to happen after bridge components are registered as the
// user-agent sends up the list of registered components in the user agent.
// `replace()` call needed until: https://github.com/hotwired/hotwire-native-android/pull/66 is merged and released into new version
Hotwire.config.userAgent = Hotwire.config.userAgentSubstring().replace("Turbo Native", "Hotwire Native")

I had mistakenly configured it at the top, above everything else (this was that way from when I was migrating from Turbo/Strada). That wont work 😅

@jayohms
Copy link
Contributor Author

jayohms commented Nov 4, 2024

@donnfelker I'll push out a new release so this PR is included in the latest release.

Note for others - When setting up Android Hotwire in your app, the user agent needs to be set after you register the bridge components

I'll think about how we can make this more better from an API perspective to avoid this issue. In the meantime, yes, you'll have to register the bridge components before setting the user agent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants