Ability to modify the WebView User-Agent
Is your feature request related to a problem? Please describe. It is not entirely straightforward to detect whether a user is using a WebView-fallback, especially if the application is replacing an older WebView-based application, or to differentiate from users opening the website in a third-party WebView-sandbox app.
Describe the solution you'd like We would like to be able to append the WebView's User-Agent with app-information, such as "MyTwaApplication/12.5.2" through the Bubblewrap-manifest:
{
...
"userAgentSuffix": "MyTwaApplication/12",
...
}
or preferably with templating for app version (which is already in the manifest):
{
...
"appVersionName": "12",
"userAgentSuffix": "MyTwaApplication/{appVersionName}",
...
}
or something similar.
Describe alternatives you've considered We have considered attempting to detect WebView using the normal user-agent, which would not provide version-information. An alternative for version might be the launch-url, but that seems to be error-prone and only limited to the initial load.
Additional context This is related to another issue I have created. I will update with the issue-number in a moment. Edit: Issue #924
Hi @JorgenMartin, thanks for filing a bug. What approaches have you tried to detect a WebView so far?
You should be able to detect it with wv token in the User-Agent like here, but I wonder if there are cases that do not set it, because we do not override it in the android browser helper.
Hi @ukaratkevich, thank you for taking the time to look into this ^.^
We have not taken the step to adopt Bubblewrap/TWA in a production application, as our internal testing resulted in a few issues, this being one of them, and as such we have not extensively explored issues detecting the wv token as you mention.
Ideally what we wanted to achieve with changing the UserAgent is to add application information to inform the website being loaded that it is in context of the TWA-application, and which version it is running. This information can then be used to inform statistics on which platforms and versions users are visiting our services from.
Additionally, detecting that the user is visiting our site from a WebView in the Bubblewrap-application allows us to inform the user that they do not have a compatible setup, as the application is now running in fallback mode. We wish to differentiate between our Bubblewrap-app and any other potential WebView-based visits, as other apps may load our service in a WebView without necessarily being in any sort of fallback-state.
This is highly relevant to #924, as both issues (in our case) is related to providing user feedback for fallback-state.
Maybe a potential alternative solution could be to include e.g. a twaFallback=true parameter when loading the fallback to inform the website of the conditions it is loaded under, or an alternative fallbackLaunchUrl for fallback?
Anyway, it would still be useful to be able to include information about what context the WebView is loaded in, e.g. the application and version. Maybe automatically appending the android application id and version could be a viable option?
Anyway, it would still be useful to be able to include information about what context the WebView is loaded in, e.g. the application and version. Maybe automatically appending the android application id and version could be a viable option?
@JorgenMartin, thanks for the clarification. This use case totally makes sense to me. While we do not support it, I can recommend for now generating a bubblewrap template and modify web view activity yourself to include all required information into the user-agent or writing your own fallback that would show a dialog from #924.
From our side I see the following proposal, which I'd prefer to discuss with a team a bit in details:
- User-Agent Client Hints - this is the most secure and convenient way so far to get what you want, e.g. server sets required keys in headers and client sends these info to the server. We do not support this neither in bubblewrap nor android-browser-helper, hopefully, there's a public API for this.
- Append web app info to the User-Agent - initially, I thought that's the most fragile and prone to fingerprinting approach, unfortunately, we can't ditch it completely, because WebView APK might not have User-Agent Client Hints support. In general, this will be a fallback anyway.
From the bubblewrap side I'd expect to have something like this:
{
"client-hints": [
"brand": {
"name": "<your_pwa_name>",
"major_version": "<version>",
...
}
]
}
This would give you additional context in combination with wv tag in the User-Agent header.
Again, this is not a final proposal, but a draft that can be used for the feature request. There's a high chance of just adding an API to append User-Agent string.