Code Monkey home page Code Monkey logo

react-native-url-polyfill's Introduction

Library's logo

React Native URL Polyfill

A lightweight and trustworthy URL polyfill for React Native

Current npm package version. Monthly downloads Current CircleCI build status. PRs welcome! React Native URL Polyfill is released under the MIT license.


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

  • Lightweight. Uses a forked version of whatwg-url (whatwg-url-without-unicode) where unicode support has been stripped out โ€” Going down from 372 KB to 40.9 KB.
  • Trustworthy. Follows closely the URL Standard spec, and relys on unit tests and Detox e2e tests within React Native.
  • Blob support. Supports React Native's Blob without additional steps.
  • Hermes support. Supports Hermes, a JavaScript engine optimized for running React Native.
  • Expo support. Supports Expo and tested against.

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 ๐Ÿ“ฆ 78.71 KB (as of RN 0.67) 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.

react-native-url-polyfill's People

Contributors

dependabot-preview[bot] avatar charpeni avatar dependabot[bot] avatar kevinhu avatar plbrault avatar thibmaek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.