Skip to content

Commit

Permalink
Merge pull request #94 from hotwired/user-agent-separation
Browse files Browse the repository at this point in the history
Separate `Hotwire.config.userAgent` from `Hotwire.config.userAgentWithWebViewDefault`
  • Loading branch information
jayohms authored Jan 29, 2025
2 parents d7bf08b + eceb83b commit 7dccb2a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
20 changes: 15 additions & 5 deletions core/src/main/kotlin/dev/hotwire/core/config/HotwireConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,30 @@ class HotwireConfig internal constructor() {
var applicationUserAgentPrefix: String? = null

/**
* Gets the full user agent that is used for every WebView instance. This includes:
* Gets the user agent that the library builds to identify the app
* and its registered bridge components. This includes:
* - Your (optional) custom `applicationUserAgentPrefix`
* - "Hotwire Native Android; Turbo Native Android;"
* - "bridge-components: [your bridge components];"
* - The WebView's default Chromium user agent string
*/
fun userAgent(context: Context): String {
val userAgent: String get() {
val components = registeredBridgeComponentFactories.joinToString(" ") { it.name }

return listOf(
applicationUserAgentPrefix,
"Hotwire Native Android; Turbo Native Android;",
"bridge-components: [$components];",
Hotwire.webViewInfo(context).defaultUserAgent
"bridge-components: [$components];"
).filterNotNull().joinToString(" ")
}

/**
* Gets the full user agent that is used for every WebView instance. This includes:
* - Your (optional) custom `applicationUserAgentPrefix`
* - "Hotwire Native Android; Turbo Native Android;"
* - "bridge-components: [your bridge components];"
* - The WebView's default Chromium user agent string
*/
fun userAgentWithWebViewDefault(context: Context): String {
return "$userAgent ${Hotwire.webViewInfo(context).defaultUserAgent}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class Session(

offlineHttpRepository.preCache(
requestHandler, OfflinePreCacheRequest(
url = location, userAgent = webView.settings.userAgentString
url = location, userAgent = Hotwire.config.userAgentWithWebViewDefault(context)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ open class HotwireWebView @JvmOverloads constructor(
id = generateViewId()
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.userAgentString = Hotwire.config.userAgent(context)
settings.userAgentString = Hotwire.config.userAgentWithWebViewDefault(context)
settings.setSupportMultipleWindows(true)
layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
initDayNightTheming()
Expand Down
25 changes: 19 additions & 6 deletions core/src/test/kotlin/dev/hotwire/core/bridge/UserAgentTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ class UserAgentTest : BaseUnitTest() {
fun `user agent with no prefix`() {
Hotwire.config.registeredBridgeComponentFactories = TestData.componentFactories

val userAgent = Hotwire.config.userAgent(context)
val userAgent = Hotwire.config.userAgent
val expectedUserAgent =
"Hotwire Native Android; Turbo Native Android; " +
"bridge-components: [one two]; " +
TEST_USER_AGENT
"bridge-components: [one two];"

assertEquals(expectedUserAgent, userAgent)
}
Expand All @@ -42,12 +41,26 @@ class UserAgentTest : BaseUnitTest() {
Hotwire.config.applicationUserAgentPrefix = "My Application Prefix;"
Hotwire.config.registeredBridgeComponentFactories = TestData.componentFactories

val userAgent = Hotwire.config.userAgent(context)
val userAgent = Hotwire.config.userAgent
val expectedUserAgent =
"My Application Prefix; " +
"Hotwire Native Android; Turbo Native Android; " +
"bridge-components: [one two]; " +
TEST_USER_AGENT
"bridge-components: [one two];"

assertEquals(expectedUserAgent, userAgent)
}

@Test
fun `user agent with prefix and webview default`() {
Hotwire.config.applicationUserAgentPrefix = "My Application Prefix;"
Hotwire.config.registeredBridgeComponentFactories = TestData.componentFactories

val userAgent = Hotwire.config.userAgentWithWebViewDefault(context)
val expectedUserAgent =
"My Application Prefix; " +
"Hotwire Native Android; Turbo Native Android; " +
"bridge-components: [one two]; " +
TEST_USER_AGENT

assertEquals(expectedUserAgent, userAgent)
}
Expand Down

0 comments on commit 7dccb2a

Please sign in to comment.