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
fix(ios): Set User-Agent in the headers #3133
base: master
Are you sure you want to change the base?
Conversation
Hello 👋, this PR has been opened for more than 2 months with no activity on it. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically |
I still believe this feature is necessary. We must acknowledge how messy the code is to support user-agent smoothly on both Android and iOS! Current Implementation (as-is): <WebView
source={{
uri: 'https://infinite.red',
...(Platform.OS === 'android' && {
headers: {
'User-Agent': 'my-custom-user-agent',
},
})
}}
style={{ width: '100%', height: '100%' }}
userAgent={Platform.OS === 'ios' ? 'my-custom-user-agent' : undefined}
injectedJavaScript={`
const div = document.createElement('div');
div.innerHTML = navigator.userAgent;
document.body.appendChild(div);
true;
`}
/> Proposed Implementation (to-be): <WebView
source={{
uri: 'https://infinite.red',
headers: {
'User-Agent': 'my-custom-user-agent',
}
}}
style={{ width: '100%', height: '100%' }}
injectedJavaScript={`
const div = document.createElement('div');
div.innerHTML = navigator.userAgent;
document.body.appendChild(div);
true;
`}
/> |
Hello 👋, this PR has been opened for more than 2 months with no activity on it. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically |
@Titozzz @jamonholmgren please any comment🥹 |
Hello 👋, this PR has been opened for more than 2 months with no activity on it. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically |
안드로이드와 동일하게, source의 header에서 User-Agent를 획득하여 웹뷰에 세팅할 수 있도록 로직을 추가합니다. ref : react-native-webview#3133 (comment)
안드로이드와 동일하게, source의 header에서 User-Agent를 획득하여 웹뷰에 세팅할 수 있도록 로직을 추가합니다. ref : react-native-webview#3133 (comment)
hello. I wrote a PR (#3106) about History API not working properly when setting userAgent on Android,
The userAgent value, which is set with the
@ReactProp
annotation, is lazy load.So I'm predicting that the userAgent is set during loading, and on reload, the history is broken.
https://developer.android.com/reference/android/webkit/WebSettings#setUserAgentString(java.lang.String)
I found the fact that it reloads in the above link.
Therefore, we decided that it would be very difficult to set the userAgent before loading the webpage because
@ReactProp
sets the userAgent and source at the same time.And I found that when I set user-agent in headers in the source prop, it also sets userAgent.
react-native-webview/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt
Lines 412 to 437 in 10105ba
In this case, userAgent works fine and history works fine because we are setting userAgent before loading.
So we need to make it work the same on IOS, to ensure hybridization.
I think it would be a good idea to deprecate the existing userAgent prop and recommend setting the userAgent in the headers.
Example Code