Home

Awesome

<p align="center"> <img height="60" src="https://user-images.githubusercontent.com/7189823/69501658-06047600-0ed5-11ea-8f54-952bf1afd68c.png" alt="Library's logo"> </p> <h3 align="center"> React Native URL Polyfill </h3> <p align="center"> A lightweight and trustworthy URL polyfill for React Native </p> <p align="center"> <a href="https://www.npmjs.org/package/react-native-url-polyfill"> <img src="https://badge.fury.io/js/react-native-url-polyfill.svg" alt="Current npm package version." /> </a> <a href="https://www.npmjs.org/package/react-native-url-polyfill"> <img src="https://img.shields.io/npm/dm/react-native-url-polyfill" alt="Monthly downloads" /> </a> <a href="https://circleci.com/gh/charpeni/react-native-url-polyfill"> <img src="https://circleci.com/gh/charpeni/react-native-url-polyfill.svg?style=shield" alt="Current CircleCI build status." /> </a> <a href="https://circleci.com/gh/charpeni/react-native-url-polyfill"> <img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" alt="PRs welcome!" /> </a> <a href="https://github.com/charpeni/react-native-url-polyfill/blob/master/LICENSE"> <img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="React Native URL Polyfill is released under the MIT license." /> </a> </p> <hr />

react-native-url-polyfill is an implementation of the WHATWG URL Standard optimized for React Native.

Why do we need this?

React Native does include a polyfill for URL, but this polyfill is homemade — in order to keep it light-weight — and was initially created to handle specific use cases.

Meanwhile, React Native has grown around that polyfill, then some unexpected errors have arisen.

Known issues (non-exhaustive) with React Native's URL are:

That's why you may need this external dependency. So, if you use URL within your app, you probably want to take a look at the installation steps below!

Unfortunately, adding react-native-url-polyfill to React Native source code would mean adding đŸ“Ļ 73.67 KB (as of RN 0.72) to the JavaScript bundle, that's why it's not included by default.

Installation

First, you need to install the polyfill, which can be done with Yarn or npm.

Yarn

yarn add react-native-url-polyfill

npm

npm install --save react-native-url-polyfill

Then, the polyfill can be used in multiple ways. Pick your preferred option.

ℹī¸ To verify if the polyfill has been correctly applied, you can check if the global variable REACT_NATIVE_URL_POLYFILL contains the current package and version like: react-native-url-polyfill@CURRENT_VERSION.

Option 1 (Simple)

Locate your JavaScript entry-point file, commonly called index.js at the root of your React Native project.

Then, import react-native-url-polyfill/auto at the top of your entry-point file, the polyfill will be automatically applied.

import 'react-native-url-polyfill/auto';

Option 2 (Flexible)

If you want to apply the polyfill when you're ready, you can import setupURLPolyfill and call it yourself.

⚠ī¸ Metro doesn't support optional imports.

If you do not apply the polyfill, it will still be added to your JavaScript bundle. Even if it's wrapped in a condition, Metro won't strip it in production.

import { setupURLPolyfill } from 'react-native-url-polyfill';

setupURLPolyfill();

Option 3 (Convenient / ponyfill)

If you prefer not to apply this polyfill over React Native's default URL, you can still import those classes manually when you want them.

import { URL, URLSearchParams } from 'react-native-url-polyfill';

const url = new URL('https://github.com');
const searchParams = new URLSearchParams('q=GitHub');

License

react-native-url-polyfill is MIT licensed.