Background Fetch

The Background Fetch component provides a powerful way to download large files in the background, even when the user closes your application. This component wraps the Background Fetch API and manages file downloads through the service worker, with automatic storage in IndexedDB for offline access. It handles download progress tracking, cancellation, and retrieval of completed downloads.

This component is particularly useful for:

  • Downloading large media files (videos, podcasts, high-resolution images)

  • Enabling offline access to content (movies, music, documents)

  • Bulk downloading of resources for offline use

  • Providing podcast or video download features

  • Creating offline-first applications with large assets

  • Building progressive web apps with downloadable content

  • Implementing reliable download managers within web applications

  • Handling file downloads that may take longer than the page lifetime

Browser Support

The Background Fetch API is a relatively new feature with limited but growing support.

Support level: Limited - Currently supported in Chromium-based browsers (Chrome, Edge, Opera) on Android and desktop. Not supported in Safari or Firefox as of early 2025.

circle-exclamation
circle-info

Background Fetch requires a registered service worker to function. Ensure your PWA has an active service worker before using this component.

Usage

Basic File Download

Multiple File Downloads

Download Management with Cancel

Retrieving Downloaded Files

Best Practices

Always Provide downloadTotal

circle-check

Use Unique IDs

circle-exclamation

Handle Browser Support Gracefully

circle-info

Always check for the unsupported event and provide fallback behavior for browsers that don't support the Background Fetch API.

Manage Storage Space

circle-exclamation

Provide User Feedback

circle-check

Icons for Better UX

Common Use Cases

1. Podcast/Music Download Manager

3. Document Library Sync

Actions

download

Starts a background fetch for one or more files.

Parameters:

  • id (string, required): Unique identifier for this download

  • url (string or array, required): URL(s) to download

  • title (string, optional): Title shown in system UI

  • icons (array, optional): Icons for system notifications

  • downloadTotal (number, optional but recommended): Total download size in bytes

cancel

Cancels an in-progress background fetch.

Parameters:

  • id (string, required): ID of the download to cancel

list

Lists all current and completed background fetches. This action is automatically called on controller connection.

Methods

has(params)

Checks if a file with the given URL is stored in IndexedDB.

Parameters:

  • params.url (string): URL to check

Returns: Promise<boolean>

get(event)

Retrieves a downloaded file from IndexedDB and opens or downloads it.

Parameters:

  • event.params.url (string): URL of the stored file

  • event.params.name (string, optional): Filename for download (if omitted, file opens in new tab)

delete(params)

Deletes a stored file from IndexedDB.

Parameters:

  • params.url (string): URL of the file to delete

getStoredFiles()

Returns all files stored in IndexedDB.

Returns: Promise<Array>

Targets

None

Events

unsupported

Fired when the Background Fetch API is not supported by the browser.

started

Fired when a background fetch starts successfully.

Event detail:

  • id: Download ID

  • urls: Array of URLs being downloaded

  • title: Download title

  • downloadTotal: Total size in bytes

in-progress

Fired periodically during download to report progress.

Event detail:

  • id: Download ID

  • downloaded: Bytes downloaded so far

  • downloadTotal: Total size in bytes

completed

Fired when a download completes successfully and is stored.

Event detail:

  • id: File URL

  • name: Filename

  • size: File size in bytes

  • contentType: MIME type

failed

Fired when a download fails.

Event detail:

  • id: Download ID

  • downloaded: Bytes downloaded before failure

  • downloadTotal: Total size

  • urls: Array of URLs

aborted

Fired when a download is successfully cancelled.

Event detail:

  • id: Download ID

exists

Fired when attempting to start a download with an ID that's already in use.

Event detail:

  • id: Download ID

  • urls: URLs of existing download

  • title: Title of existing download

  • downloadTotal: Total size

not-found

Fired when attempting to cancel a download that doesn't exist.

Event detail:

  • id: Download ID

cancel-refused

Fired when a download cannot be cancelled (already finished or failed).

Event detail:

  • id: Download ID

  • reason: Reason why cancellation was refused

Resources

Last updated

Was this helpful?