-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add background fetch support to service worker
This commit introduces background fetch functionality, including DTOs, service worker rules, and a Stimulus controller for handling fetch progress and completion. It also updates the service worker configuration to enable caching, progress, success URLs, and customizable messages for success and failure scenarios.
- Loading branch information
Showing
7 changed files
with
312 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
import { Controller } from '@hotwired/stimulus'; | ||
|
||
/* stimulusFetch: 'lazy' */ | ||
export default class extends Controller { | ||
download = async ({params}) => { | ||
const { id, requests, title, icons, downloadTotal } = params; | ||
if (!requests) { | ||
return; | ||
} | ||
const processId = id || self.crypto.randomUUID(); | ||
await this.fetch(processId, requests, { | ||
title, | ||
icons, | ||
downloadTotal | ||
}); | ||
} | ||
|
||
fetch = async (id, requests, options) => { | ||
try { | ||
const registration = await navigator.serviceWorker.ready; | ||
const bgFetch = await registration.backgroundFetch.fetch(id, requests, options); | ||
bgFetch.addEventListener('progress', () => this.dispatchStatus(bgFetch)); | ||
|
||
} catch (error) { | ||
this.dispatchEvent('error', { error }); | ||
} | ||
} | ||
|
||
status = async () => { | ||
const registration = await navigator.serviceWorker.ready; | ||
const ids = await registration.backgroundFetch.getIds(); | ||
this.dispatchEvent('ids', ids); | ||
const promises = ids.map(async (id) => { | ||
const bgFetch = await registration.backgroundFetch.get(id); | ||
if (!bgFetch) { | ||
return; | ||
} | ||
bgFetch.addEventListener('progress', () => this.dispatchStatus(bgFetch)); | ||
}); | ||
await Promise.all(promises); | ||
} | ||
|
||
dispatchStatus = (bgFetch) => { | ||
this.dispatchEvent('status', { | ||
id: bgFetch.id, | ||
uploadTotal: bgFetch.uploadTotal, | ||
uploaded: bgFetch.uploaded, | ||
downloadTotal: bgFetch.downloadTotal, | ||
downloaded: bgFetch.downloaded, | ||
result: bgFetch.result, | ||
failureReason: bgFetch.failureReason, | ||
recordsAvailable: bgFetch.recordsAvailable, | ||
}); | ||
} | ||
|
||
dispatchEvent = (name, payload) => { | ||
this.dispatch(name, { detail: payload }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\Dto; | ||
|
||
use Symfony\Component\Serializer\Attribute\SerializedName; | ||
|
||
final class BackgroundFetch | ||
{ | ||
public bool $enabled; | ||
|
||
#[SerializedName('cache_name')] | ||
public string $cacheName; | ||
|
||
#[SerializedName('progress_url')] | ||
public null|Url $progressUrl = null; | ||
|
||
#[SerializedName('success_url')] | ||
public null|Url $successUrl = null; | ||
|
||
#[SerializedName('success_message')] | ||
public null|string $successMessage = null; | ||
|
||
#[SerializedName('failure_message')] | ||
public null|string $failureMessage = null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.