From 4a809720ca4b7f750f702455a3b915408331505a Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Wed, 7 Feb 2024 20:13:15 +0100 Subject: [PATCH] Skip Waiting support (#61) --- src/DependencyInjection/Configuration.php | 4 ++ src/Dto/ServiceWorker.php | 3 ++ src/Service/ServiceWorkerCompiler.php | 60 +++++++++++++++-------- 3 files changed, 46 insertions(+), 21 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 248b331..ea290c3 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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() diff --git a/src/Dto/ServiceWorker.php b/src/Dto/ServiceWorker.php index 7863830..844962d 100644 --- a/src/Dto/ServiceWorker.php +++ b/src/Dto/ServiceWorker.php @@ -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; } diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index b395c3e..5d4bf5d 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -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 = <<processOfflineFallback($workbox, $body); } + private function processWorkboxImport(Workbox $workbox, string $body): string + { + if (! str_contains($body, $workbox->workboxImportPlaceholder)) { + return $body; + } + if ($workbox->useCDN === true) { + $declaration = <<version}/workbox-sw.js'); +IMPORT_CDN_STRATEGY; + } else { + $publicUrl = '/' . trim($workbox->workboxPublicUrl, '/'); + $declaration = <<workboxImportPlaceholder, trim($declaration), $body); + } + private function processStandardRules(Workbox $workbox, string $body): string { if (! str_contains($body, $workbox->standardRulesPlaceholder)) { @@ -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 = <<version}/workbox-sw.js'); -IMPORT_CDN_STRATEGY; - } else { - $publicUrl = '/' . trim($workbox->workboxPublicUrl, '/'); - $declaration = <<workboxImportPlaceholder, trim($declaration), $body); - } }