react-native-url-polyfill
repository·main·Indexed 19 days ago
https://github.com/charpeni/react-native-url-polyfillA lightweight, WHATWG-compliant URL polyfill for React Native version 4.0.0. It provides a reliable implementation of URL and URLSearchParams supporting Hermes, Expo, and Blob. The library can be applied automatically via 'react-native-url-polyfill/auto', manually using setupURLPolyfill(), or used as a ponyfill by importing classes directly. Note: Non-ASCII characters are not supported in hostnames.
What's inside react-native-url-polyfill
- To maintain a lightweight bundle size, this polyfill has stripped out Unicode support for hostnames. Non-ASCII characters are not supported in the hostname.
Build and run the Android app
mainWith Metro running, open a new terminal window from the root of your project and run the following to build and launch the Android application:
# Using npm npm run android # OR using Yarn yarn androidnpm run android # OR yarn androidBuild and run the iOS app
mainTo run the iOS application, you must first ensure CocoaPods dependencies are installed.
Install CocoaPods dependencies:
- If this is your first time or you have updated native dependencies, run:
bundle exec pod install - If you are setting up the Ruby environment for the first time, run:
bundle install
- If this is your first time or you have updated native dependencies, run:
Run the app:
# Using npm npm run ios # OR using Yarn yarn ios
bundle exec pod install # THEN npm run ios # OR yarn iosApply the polyfill automatically (Simple)
mainTo automatically apply the polyfill globally, import
react-native-url-polyfill/autoat the very top of your JavaScript entry-point file (usuallyindex.js). This method is the simplest way to ensureURLandURLSearchParamsare available throughout your application.Note: On web platforms, this import acts as a no-op.
import 'react-native-url-polyfill/auto';Use as a ponyfill (Convenient)
mainIf you do not want to overwrite the default React Native
URLimplementation, you can use this library as a ponyfill by importing the specific classes you need. This allows you to use the WHATWG-compliant implementation alongside the existing globalURL.import { URL, URLSearchParams } from 'react-native-url-polyfill'; const url = new URL('https://github.com'); const searchParams = new URLSearchParams('q=GitHub');Install react-native-url-polyfill
mainInstall the polyfill using your preferred package manager (e.g., npm or Yarn) before choosing an integration method.
yarn add react-native-url-polyfillStart your Android or iOS application
mainOnce the Metro Bundler is running in its own terminal, open a new terminal from the root of your project to launch the application on your emulator or simulator.
# For Android # using npm npm run android # OR using Yarn yarn android # For iOS # using npm npm run ios # OR using Yarn yarn iosReload your application to see changes
mainAfter modifying your code (e.g., in
App.tsx), you can reload the application to apply changes using the following platform-specific methods:- Android: Press the <kbd>R</kbd> key twice or open the Developer Menu (<kbd>Ctrl</kbd> + <kbd>M</kbd> on Windows/Linux or <kbd>Cmd ⌘</kbd> + <kbd>M</kbd> on macOS) and select "Reload".
- iOS: Press <kbd>Cmd ⌘</kbd> + <kbd>R</kbd> in the iOS Simulator.
Reload the application
mainWhile Fast Refresh automatically updates your app when you save files, you can perform a manual full reload to reset the application state:
- Android: Press the <kbd>R</kbd> key twice, or open the Dev Menu via <kbd>Ctrl</kbd> + <kbd>M</kbd> (Windows/Linux) or <kbd>Cmd ⌘</kbd> + <kbd>M</kbd> (macOS) and select "Reload".
- iOS: Press <kbd>R</kbd> in the iOS Simulator.
Start the Metro bundler
mainBefore building your app, you must start Metro, the JavaScript build tool for React Native. Run the following command from the root of your project:
# Using npm npm start # OR using Yarn yarn startnpm start # OR yarn startApply the polyfill manually (Flexible)
mainIf you need control over exactly when the polyfill is applied, import
setupURLPolyfilland invoke it manually in your code.import { setupURLPolyfill } from 'react-native-url-polyfill'; setupURLPolyfill();Verify the polyfill installation
mainTo confirm that the polyfill has been correctly applied and is active, check the global variableREACT_NATIVE_URL_POLYFILL. It should contain a string in the format:react-native-url-polyfill@CURRENT_VERSION.