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.
Always check for Background Fetch API support before using this component. The component will emit an unsupported event when the API is not available.
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
Always specify the downloadTotal parameter when starting a background fetch. This allows the browser to display accurate progress information to the user in the system UI.
Use Unique IDs
Each background fetch requires a unique ID. If you attempt to start a fetch with an ID that's already in use, the component will emit an exists event instead of starting a new download.
Handle Browser Support Gracefully
Always check for the unsupported event and provide fallback behavior for browsers that don't support the Background Fetch API.
Manage Storage Space
Background-fetched files are stored in IndexedDB, which has storage limits. Implement cleanup strategies to manage storage space:
Delete old downloads when new ones complete
Provide UI for users to manually delete downloads
Monitor storage quota usage
Provide User Feedback
The Background Fetch API shows system-level notifications, but always provide in-app UI feedback as well for a better user experience.
Icons for Better UX
Common Use Cases
1. Podcast/Music Download Manager
3. Document Library Sync
4. Media Gallery Offline Mode
Actions
download
downloadStarts a background fetch for one or more files.
Parameters:
id(string, required): Unique identifier for this downloadurl(string or array, required): URL(s) to downloadtitle(string, optional): Title shown in system UIicons(array, optional): Icons for system notificationsdownloadTotal(number, optional but recommended): Total download size in bytes
cancel
cancelCancels an in-progress background fetch.
Parameters:
id(string, required): ID of the download to cancel
list
listLists all current and completed background fetches. This action is automatically called on controller connection.
Methods
has(params)
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)
get(event)Retrieves a downloaded file from IndexedDB and opens or downloads it.
Parameters:
event.params.url(string): URL of the stored fileevent.params.name(string, optional): Filename for download (if omitted, file opens in new tab)
delete(params)
delete(params)Deletes a stored file from IndexedDB.
Parameters:
params.url(string): URL of the file to delete
getStoredFiles()
getStoredFiles()Returns all files stored in IndexedDB.
Returns: Promise<Array>
Targets
None
Events
unsupported
unsupportedFired when the Background Fetch API is not supported by the browser.
started
startedFired when a background fetch starts successfully.
Event detail:
id: Download IDurls: Array of URLs being downloadedtitle: Download titledownloadTotal: Total size in bytes
in-progress
in-progressFired periodically during download to report progress.
Event detail:
id: Download IDdownloaded: Bytes downloaded so fardownloadTotal: Total size in bytes
completed
completedFired when a download completes successfully and is stored.
Event detail:
id: File URLname: Filenamesize: File size in bytescontentType: MIME type
failed
failedFired when a download fails.
Event detail:
id: Download IDdownloaded: Bytes downloaded before failuredownloadTotal: Total sizeurls: Array of URLs
aborted
abortedFired when a download is successfully cancelled.
Event detail:
id: Download ID
exists
existsFired when attempting to start a download with an ID that's already in use.
Event detail:
id: Download IDurls: URLs of existing downloadtitle: Title of existing downloaddownloadTotal: Total size
not-found
not-foundFired when attempting to cancel a download that doesn't exist.
Event detail:
id: Download ID
cancel-refused
cancel-refusedFired when a download cannot be cancelled (already finished or failed).
Event detail:
id: Download IDreason: Reason why cancellation was refused
Related Components
Service Worker - Required for Background Fetch to function
BackgroundSync Queue - Queue requests for later execution
Sync Broadcast - Broadcast messages between tabs and service worker
Resources
Last updated
Was this helpful?