Code
Browser Web APIs
Allows web applications to redirect audio output to a permitted Bluetooth headset, speakerphone, or other device.
Provides a way for browsers to better manage audio sessions and inform browsers about what kind of audio is being played.
Provides a method for managing downloads that may take a significant amount of time, running in the background if the user navigates away.
Enables web apps to defer tasks to be run in a service worker once the user has a stable network connection.
Allows scheduling of non-critical work to be performed during idle periods via requestIdleCallback.
Allows installed web apps to set a badge on the app icon to notify users of pending activity such as messages or updates.
Detects linear and two-dimensional barcodes, including QR codes, in images and video streams.
Provides information about the battery charge level of the device and notifications when the level or status changes.
Sends asynchronous, non-blocking requests to a server without expecting a response, ideal for analytics and diagnostics.
Enables simple communication between browsing contexts such as tabs, windows, and iframes sharing the same origin.
Background Fetch API
ExperimentalProvides a method for managing large downloads that may take significant time. Unlike the Fetch API, downloads continue even if the user navigates away or closes the browser, with progress surfaced via a browser UI element.
Key Interfaces
ServiceWorkerRegistration.backgroundFetch— Returns aBackgroundFetchManagerfor the registration.BackgroundFetchManager.fetch(id, requests, options)— Starts a background fetch and returns aBackgroundFetchRegistration.BackgroundFetchManager.get(id)— Returns an existingBackgroundFetchRegistrationby ID.BackgroundFetchRegistration— Tracks state, progress (downloaded/downloadTotal), and result of the fetch.BackgroundFetchRecord— Represents an individual request/response pair within the fetch.
Example — Start a background fetch
Initiate a background fetch for a large file from the page. The browser shows download progress to the user even if the tab is closed.
const registration = await navigator.serviceWorker.ready;
const bgFetch = await registration.backgroundFetch.fetch(
'my-download-id',
['/videos/episode-1.mp4'],
{
title: 'Episode 1',
icons: [{ src: '/icon-512.png', sizes: '512x512', type: 'image/png' }],
downloadTotal: 60 * 1024 * 1024, // 60 MB hint
}
);
console.log('Fetch started:', bgFetch.id);
console.log('Result:', bgFetch.result); // "" | "success" | "failure"Example — Handle completion in the service worker
Listen for backgroundfetchsuccess in the service worker to cache the downloaded responses.
// service-worker.js
self.addEventListener('backgroundfetchsuccess', (event) => {
const bgFetch = event.registration;
event.waitUntil(async function () {
const cache = await caches.open('downloads');
const records = await bgFetch.matchAll();
const promises = records.map(async (record) => {
const response = await record.responseReady;
await cache.put(record.request, response);
});
await Promise.all(promises);
event.updateUI({ title: 'Download complete!' });
}());
});
self.addEventListener('backgroundfetchfail', (event) => {
console.error('Background fetch failed:', event.registration.id);
});Example — Track download progress
Poll or listen to the registration object to display progress in the UI.
const registration = await navigator.serviceWorker.ready;
const bgFetch = await registration.backgroundFetch.get('my-download-id');
if (bgFetch) {
bgFetch.addEventListener('progress', () => {
if (!bgFetch.downloadTotal) return;
const percent = Math.round(
(bgFetch.downloaded / bgFetch.downloadTotal) * 100
);
console.log(`Downloaded ${percent}%`);
});
}