react-native: Creating an instance of URL like: `new URL('http://facebook.com')` throws an exception.

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

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 44
  • Comments: 39 (6 by maintainers)

Commits related to this issue

Most upvoted comments

@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…

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;

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. If you think this issue should definitely remain open, please let us know why. Thank you for your contributions.

requiring the module seemed to work for me:

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

Hi! Our implementation of URL is narrow to avoid the bundle size impact of the more spec-complete implementation. Is there a library dependency you have that uses the URL constructor in this way?

If you can be more specific with what your code is trying to do, I can make a suggestion on the PR.

I’m trying to parse a URL. The URL class is perfect for that. I don’t see anything in the docs mentioning you’ve gutted URL for “bundle size” reasons.

if (new URL(url, baseURL).origin === baseURL && !headers.Authorization) {
  headers.Authorization = `Token ${token}`
}

What’s really cool about that is that is that it works just fine in testing because jest is running in a completely different environment to what the app actually will, so I’ve had something in master for about a week that simply breaks immediately.

Could y’all fix this, please? The way I see it, URL, being pretty standard and built in, isn’t going to change much. Likely means the first time someone downloads an app will be the last time they download URL, so bundle size be damned - that always takes ages anyway. At least let me, as a developer, opt in to the butchery.

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

not stale bot! Really annoying this issue! =\

Any updates? This is still an issue in 0.56 …

Sad, another basic feature missing.

Any updates on this??

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

+1 any update?

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

Still seeing this. RN version "^0.58.6"

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