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

Creating an instance of URL like: new URL('http://facebook.com') throws an exception. #16434

Closed
ipeychev opened this issue Oct 17, 2017 · 39 comments
Assignees

Comments

@ipeychev
Copy link

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

Environment:
OS: macOS Sierra 10.12.6
Node: 8.6.0
Yarn: 0.24.6
npm: 5.3.0
Watchman: 4.7.0
Xcode: Xcode 8.3 Build version 8E162
Android Studio: 2.3 AI-162.3934792

Packages: (wanted => installed)
react: 16.0.0-alpha.12 => 16.0.0-alpha.12
react-native: ^0.48.4 => 0.48.4
Target Platform: iOS (10.3)

Steps to Reproduce

  1. Create a ReactNative project, which uses React Native 16
  2. Try to create an instance of URL in any .js file, like this:
    const url = new URL('http://facebook.com');
  3. Run the project

Expected Behavior

The URL to be the native URL, this one. It was working fine in React 15, but in React 16 it was replaced with something internal. We use URL to parse URLs and extract information about the hostname, port, etc. so we need this behaviour back.

Actual Behavior

An exception is thrown: Creating BlobURL objects is not supported yet.

screen shot 2017-10-17 at 18 15 19

Reproducible Demo

https://snack.expo.io/SJUA5i7TZ

@ipeychev ipeychev changed the title Creating an instance of URL like new URL('http://facebook.com') throws an exception. Creating an instance of URL like: new URL('http://facebook.com') throws an exception. Oct 17, 2017
@phillbaker
Copy link

requiring the module seemed to work for me:

const url = require('url');
console.log(url.parse('https://example.com'));

@mloureiro
Copy link

@phillbaker what @ipeychev meant is the URL definition: https://developer.mozilla.org/en-US/docs/Web/API/URL
not a custom module

@stale

This comment has been minimized.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Feb 18, 2018
@Panthro

This comment has been minimized.

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Feb 24, 2018
@ck-kc

This comment has been minimized.

@Trenrod
Copy link

Trenrod commented Feb 26, 2018

Reproduced with:
Environment:
OS: Windows 10
Node: 6.12.3
Yarn: Not Found
npm: 3.10.10
Watchman: Not Found
Xcode: N/A
Android Studio: Version 3.0.0.0 AI-171.4443003

Packages: (wanted => installed)
react: 16.2.0 => 16.2.0
react-native: 0.53.3 => 0.53.3

@lxcid
Copy link

lxcid commented Mar 11, 2018

Reproduced in:

Environment:
  OS: macOS High Sierra 10.13.2
  Node: 9.2.0
  Yarn: 1.3.2
  npm: 5.5.1
  Watchman: 4.9.0
  Xcode: Xcode 9.2 Build version 9C40b
  Android Studio: 2.3 AI-162.4069837

Packages: (wanted => installed)
  react: 16.2.0 => 16.2.0
  react-native: 0.53.2 => 0.53.2

@m-vdb
Copy link

m-vdb commented Mar 28, 2018

Having the issue as well, so sad :(

Environment:
  OS: macOS Sierra 10.12.6
  Node: 6.11.2
  Yarn: Not Found
  npm: 5.3.0
  Watchman: 4.5.0
  Xcode: Xcode 9.2 Build version 9C40b
  Android Studio: 3.0 AI-171.4443003

Packages: (wanted => installed)
  react: 16.2.0 => 16.2.0
  react-native: 0.52.2 => 0.52.2

@ovy9086

This comment has been minimized.

@andrenerd

This comment has been minimized.

@benjohnson2001

This comment has been minimized.

1 similar comment
@danleechina

This comment has been minimized.

@mike-marcacci
Copy link
Contributor

This can be worked around. Add this little piece of nastiness to your index.js:

// see https://github.com/facebook/react-native/issues/14796
import { Buffer } from "buffer";
global.Buffer = Buffer;

// see https://github.com/facebook/react-native/issues/16434
import { URL, URLSearchParams } from "whatwg-url";
global.URL = URL;
global.URLSearchParams = URLSearchParams;

@ricardochen

This comment has been minimized.

@kodayashi
Copy link

Same here. This would be nice to use when parsing URLs for deep link routing.

@heronlyj

This comment has been minimized.

@idotial
Copy link

idotial commented Jun 15, 2018

i have the same problem while using web3 in websocketProvider. new URL() write in web3 source code, i can't change it. Any solution?

@bryce-larson

This comment has been minimized.

2 similar comments
@kharenzze

This comment has been minimized.

@doublex

This comment has been minimized.

@ghost

This comment has been minimized.

@itskevinsam

This comment has been minimized.

@tuncaulubilge
Copy link
Contributor

React-native seems to replace URL with this one, to support local blob urls: https://github.com/facebook/react-native/blob/master/Libraries/Blob/URL.js

@charpeni
Copy link
Contributor

👋 You should check this out: https://github.com/charpeni/react-native-url-polyfill.

@hramos
Copy link
Contributor

hramos commented Jan 30, 2020

Closing as a workaround is provided in #16434 (comment)

@hramos hramos closed this as completed Jan 30, 2020
chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this issue Jun 9, 2020
React Native does have its own built-in polyfill for the URL Web API
[1], but it's so minimal that it has some serious bugs, and a React
Native maintainer has even recommended `react-native-url-polyfill`
as an official workaround for one of those bugs [2].

Many thanks to charpeni for the changes [2] that allow RN v0.60
support! And for the changes from our own PR
charpeni/whatwg-url#3, to avoid touching the
Buffer global.

This commit uses a release candidate version; there will likely be
an official release soon.

[1]: https://developer.mozilla.org/en-US/docs/Web/API/URL

[2]: facebook/react-native#16434 (comment)

[3]: charpeni/react-native-url-polyfill@d4e26ce#diff-375a2b7b7d929526f0dd58cd8bb8665b

Fixes: zulip#4081
chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this issue Jun 9, 2020
React Native does have its own built-in polyfill for the URL Web API
[1], but it's so minimal that it has some serious bugs, and a React
Native maintainer has even recommended `react-native-url-polyfill`
as an official workaround for one of those bugs [2].

Many thanks to charpeni for the changes [2] that allow RN v0.60
support! And for the changes from our own PR
charpeni/whatwg-url#3, to avoid touching the
Buffer global.

This commit uses a release candidate version; there will likely be
an official release soon.

[1]: https://developer.mozilla.org/en-US/docs/Web/API/URL

[2]: facebook/react-native#16434 (comment)

[3]: charpeni/react-native-url-polyfill@d4e26ce#diff-375a2b7b7d929526f0dd58cd8bb8665b

Fixes: zulip#4081
gnprice pushed a commit to chrisbobbe/zulip-mobile that referenced this issue Jun 9, 2020
React Native does have its own built-in polyfill for the URL Web API
[1], but it's so minimal that it has some serious bugs, and a React
Native maintainer has even recommended `react-native-url-polyfill`
as an official workaround for one of those bugs [2].

Many thanks to charpeni for the changes [3] that allow RN v0.60
support! And for the changes from our own PR
charpeni/whatwg-url#3, to avoid touching the
Buffer global.

This commit uses a release candidate version; there will likely be
an official release soon.

[1]: https://developer.mozilla.org/en-US/docs/Web/API/URL

[2]: facebook/react-native#16434 (comment)

[3]: charpeni/react-native-url-polyfill@d4e26ce#diff-375a2b7b7d929526f0dd58cd8bb8665b

Fixes: zulip#4081
@Nantris
Copy link

Nantris commented Sep 18, 2021

@hramos if a polyfill existing is reason enough to close this issue then I think the React Native team should deprecate the URL module and outsource it to the community as they have with others. This weird, multi-year, limbo state of the URL module is not good...

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