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

fix(ios): Set User-Agent in the headers #3133

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

gronxb
Copy link
Contributor

@gronxb gronxb commented Sep 9, 2023

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.

if (source.hasKey("headers")) {
if (newArch) {
val headerArray = source.getArray("headers");
for (header in headerArray!!.toArrayList()) {
val headerCasted = header as HashMap<String, String>
val name = headerCasted.get("name") ?: ""
val value = headerCasted.get("value") ?: ""
if ("user-agent" == name.lowercase(Locale.ENGLISH)) {
view.settings.userAgentString = value
} else {
headerMap[name] = value
}
}
} else {
val headers = source.getMap("headers")
val iter = headers!!.keySetIterator()
while (iter.hasNextKey()) {
val key = iter.nextKey()
if ("user-agent" == key.lowercase(Locale.ENGLISH)) {
view.settings.userAgentString = headers.getString(key)
} else {
headerMap[key] = headers.getString(key)
}
}
}
}

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

import React, { Component } from 'react';
import { View } from 'react-native';

import WebView from 'react-native-webview';

interface Props {}
interface State {}

export default class NativeWebpage extends Component<Props, State> {
  state = {};

  render() {
    return (
      <View style={{ height: 400 }}>
        <WebView
          source={{
            uri: 'https://infinite.red',
            headers: {
              // This Field !
              'User-Agent': 'Test Header',
            },
          }}
          style={{ width: '100%', height: '100%' }}
          injectedJavaScript={`
              const div = document.createElement('div');
              div.innerHTML = navigator.userAgent;

              document.body.appendChild(div);
              true;

            `}
         />
      </View>
    );
  }
}

Copy link

github-actions bot commented Nov 9, 2023

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

@github-actions github-actions bot added the Stale label Nov 9, 2023
@gronxb
Copy link
Contributor Author

gronxb commented Nov 9, 2023

@Titozzz @jamonholmgren

Issues

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;
  `}
/>

@github-actions github-actions bot removed the Stale label Nov 10, 2023
Copy link

github-actions bot commented Jan 9, 2024

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

@github-actions github-actions bot added the Stale label Jan 9, 2024
@gronxb
Copy link
Contributor Author

gronxb commented Jan 9, 2024

@Titozzz @jamonholmgren please any comment🥹

@github-actions github-actions bot removed the Stale label Jan 10, 2024
@gronxb gronxb changed the title feat(ios): Set User-Agent in the headers fix(ios): Set User-Agent in the headers Jan 18, 2024
Copy link

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

@github-actions github-actions bot added the Stale label Mar 19, 2024
@github-actions github-actions bot closed this Mar 28, 2024
@Titozzz Titozzz removed the Stale label Apr 1, 2024
@Titozzz Titozzz reopened this Apr 1, 2024
ujeon added a commit to classtinginc/react-native-webview that referenced this pull request Apr 17, 2024
안드로이드와 동일하게, source의 header에서 User-Agent를 획득하여 웹뷰에 세팅할 수 있도록 로직을 추가합니다.

ref : react-native-webview#3133 (comment)
ujeon added a commit to classtinginc/react-native-webview that referenced this pull request Apr 17, 2024
안드로이드와 동일하게, source의 header에서 User-Agent를 획득하여 웹뷰에 세팅할 수 있도록 로직을 추가합니다.

ref : react-native-webview#3133 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants