Fetch utilities
import { forceFetchAsXHR, fetch, Headers, Request, Response, DOMException } from '@bbc/front-end-kit/js/utils/fetch';
This file contains the ability to launch a polyfill for the browser's Fetch API on demand. The kit provides this mainly for Showpad projects so they can run on Android without conflicting with the securities of the File protocol, which blocks Fetch API functionalities. XHR is still available. Thanks to this utility, we are able to run our Showpad experiences using modern JavaScript without the need to refactor the codebase for Android.
Side effects on import
Importing this file installs g.fetch, g.Headers, g.Request, and g.Response onto the global object if they are not already present (g resolves to globalThis, self, global, or {}). This happens at module evaluation time, before any function is called.
forceFetchAsXHR(condition, logMessage)
Forces the Fetch API to use XHR under the hood by replacing window.fetch with the bundled XHR-backed polyfill — even when the browser already supports native fetch. Useful for Showpad Android environments where the File protocol blocks native fetch.
Parameters
condition(boolean, defaulttrue): Only applies the override when truthy. Useful for targeting specific environments.logMessage(string, optional): Message logged to the console on activation. Defaults to a generic override notification.
Returns
undefined
import { forceFetchAsXHR } from '@bbc/front-end-kit/js/utils/fetch';
// always override
forceFetchAsXHR();
// only override on Android
forceFetchAsXHR(
navigator.userAgent.toLowerCase().includes('android'),
'Fetch API override activated: Android detected'
);
fetch(input, init)
The XHR-backed fetch implementation (a modified port of github/fetch). Has the same signature as the native Fetch API. Used internally by forceFetchAsXHR and installed globally when native fetch is absent.
Headers
Fetch-compatible Headers constructor. Supports append, delete, get, has, set, forEach, keys, values, entries.
Request
Fetch-compatible Request constructor. Must be called with new. Supports clone().
Response
Fetch-compatible Response constructor. Must be called with new. Supports clone(), Response.error(), Response.redirect(url, status).
DOMException
Re-export of the global DOMException, with a polyfill fallback for environments where it is unavailable.