Skip to content

Commit

Permalink
Skip Waiting support
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Feb 7, 2024
1 parent e479f10 commit 08dafeb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ private function setupServiceWorker(ArrayNodeDefinition $node): void
->info('The public URL to the service worker.')
->example('/sw.js')
->end()
->booleanNode('skip_waiting')
->defaultFalse()
->info('Whether to skip waiting for the service worker to be activated.')
->end()
->arrayNode('workbox')
->info('The configuration of the workbox.')
->canBeDisabled()
Expand Down
3 changes: 3 additions & 0 deletions src/Dto/ServiceWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ final class ServiceWorker
#[SerializedName('use_cache')]
public null|bool $useCache = null;

#[SerializedName('skip_waiting')]
public bool $skipWaiting = false;

public Workbox $workbox;
}
60 changes: 39 additions & 21 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,25 @@ public function compile(): ?string
$body = $this->processWorkbox($workbox, $body);
}

return $body;
return $this->processSkipWaiting($body);
}

private function processSkipWaiting(string $body): string
{
if ($this->serviceWorker->skipWaiting === false) {
return $body;
}

$declaration = <<<SKIP_WAITING
self.addEventListener("install", function (event) {
event.waitUntil(self.skipWaiting());
});
self.addEventListener("activate", function (event) {
event.waitUntil(self.clients.claim());
});
SKIP_WAITING;

return $body . trim($declaration);
}

private function processWorkbox(Workbox $workbox, string $body): string
Expand All @@ -63,6 +81,26 @@ private function processWorkbox(Workbox $workbox, string $body): string
return $this->processOfflineFallback($workbox, $body);
}

private function processWorkboxImport(Workbox $workbox, string $body): string
{
if (! str_contains($body, $workbox->workboxImportPlaceholder)) {
return $body;
}
if ($workbox->useCDN === true) {
$declaration = <<<IMPORT_CDN_STRATEGY
importScripts('https://storage.googleapis.com/workbox-cdn/releases/{$workbox->version}/workbox-sw.js');
IMPORT_CDN_STRATEGY;
} else {
$publicUrl = '/' . trim($workbox->workboxPublicUrl, '/');
$declaration = <<<IMPORT_CDN_STRATEGY
importScripts('{$publicUrl}/workbox-sw.js');
workbox.setConfig({modulePathPrefix: '{$publicUrl}'});
IMPORT_CDN_STRATEGY;
}

return str_replace($workbox->workboxImportPlaceholder, trim($declaration), $body);
}

private function processStandardRules(Workbox $workbox, string $body): string
{
if (! str_contains($body, $workbox->standardRulesPlaceholder)) {
Expand Down Expand Up @@ -241,24 +279,4 @@ private function processWidgets(Workbox $workbox, string $body): string

return str_replace($workbox->widgetsPlaceholder, trim($declaration), $body);
}

private function processWorkboxImport(Workbox $workbox, string $body): string
{
if (! str_contains($body, $workbox->workboxImportPlaceholder)) {
return $body;
}
if ($workbox->useCDN === true) {
$declaration = <<<IMPORT_CDN_STRATEGY
importScripts('https://storage.googleapis.com/workbox-cdn/releases/{$workbox->version}/workbox-sw.js');
IMPORT_CDN_STRATEGY;
} else {
$publicUrl = '/' . trim($workbox->workboxPublicUrl, '/');
$declaration = <<<IMPORT_CDN_STRATEGY
importScripts('{$publicUrl}/workbox-sw.js');
workbox.setConfig({modulePathPrefix: '{$publicUrl}'});
IMPORT_CDN_STRATEGY;
}

return str_replace($workbox->workboxImportPlaceholder, trim($declaration), $body);
}
}

0 comments on commit 08dafeb

Please sign in to comment.