From 1442a4f894a39577b7c3a0d95c8cebad858fcd2d Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Thu, 21 Mar 2024 07:33:10 +0100 Subject: [PATCH] features: CachePlugin and CacheStrategy are now interfaces --- src/CachingStrategy/WorkboxCacheStrategy.php | 5 +- src/Service/ServiceWorkerCompiler.php | 8 ++- .../AppendCacheStrategies.php | 2 +- src/ServiceWorkerRule/ClearCache.php | 30 +++++++-- src/ServiceWorkerRule/OfflineFallback.php | 67 +++++++++++++------ src/ServiceWorkerRule/SkipWaiting.php | 27 +++++++- src/ServiceWorkerRule/WindowsWidgets.php | 49 ++++++++++++-- src/ServiceWorkerRule/WorkboxImport.php | 43 ++++++++++-- 8 files changed, 190 insertions(+), 41 deletions(-) diff --git a/src/CachingStrategy/WorkboxCacheStrategy.php b/src/CachingStrategy/WorkboxCacheStrategy.php index 7fb4ec7..ad984f4 100644 --- a/src/CachingStrategy/WorkboxCacheStrategy.php +++ b/src/CachingStrategy/WorkboxCacheStrategy.php @@ -145,10 +145,11 @@ public function render(string $cacheObjectName, bool $debug = false): string } $declaration .= <<strategy}({ {$timeout}{$cacheName}plugins: {$plugins} }); +// Register the route with the Workbox Router workbox.routing.registerRoute({$this->matchCallback},{$cacheObjectName}{$method}); ROUTE_REGISTRATION; @@ -156,6 +157,7 @@ public function render(string $cacheObjectName, bool $debug = false): string if ($this->preloadUrls !== []) { $fontUrls = json_encode($this->preloadUrls, $jsonOptions); $declaration .= << { const done = {$fontUrls}.map( path => @@ -175,6 +177,7 @@ public function render(string $cacheObjectName, bool $debug = false): string /**************************************************** END CACHE STRATEGY ****************************************************/ + DEBUG_STATEMENT; return $debug === true ? $declaration : trim($declaration); diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index 76b09ff..dda6476 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -24,6 +24,8 @@ public function __construct( private AssetMapperInterface $assetMapper, #[TaggedIterator('spomky_labs_pwa.service_worker_rule', defaultPriorityMethod: 'getPriority')] private iterable $serviceworkerRules, + #[Autowire('%kernel.debug%')] + public bool $debug, ) { } @@ -35,7 +37,11 @@ public function compile(): ?string $body = ''; foreach ($this->serviceworkerRules as $rule) { - $body .= $rule->process(); + $ruleBody = $rule->process($this->debug); + if ($this->debug === false) { + $ruleBody = trim($ruleBody); + } + $body .= $ruleBody; } return $body . $this->includeRootSW(); diff --git a/src/ServiceWorkerRule/AppendCacheStrategies.php b/src/ServiceWorkerRule/AppendCacheStrategies.php index 15133e3..d4c06f4 100644 --- a/src/ServiceWorkerRule/AppendCacheStrategies.php +++ b/src/ServiceWorkerRule/AppendCacheStrategies.php @@ -22,7 +22,7 @@ public function __construct( ) { } - public function process(): string + public function process(bool $debug = false): string { $body = ''; foreach ($this->cacheStrategies as $idCacheStrategy => $cacheStrategy) { diff --git a/src/ServiceWorkerRule/ClearCache.php b/src/ServiceWorkerRule/ClearCache.php index d11b0a0..4ecc653 100644 --- a/src/ServiceWorkerRule/ClearCache.php +++ b/src/ServiceWorkerRule/ClearCache.php @@ -12,12 +12,12 @@ private Workbox $workbox; public function __construct( - ServiceWorker $serviceWorker + ServiceWorker $serviceWorker, ) { $this->workbox = $serviceWorker->workbox; } - public function process(): string + public function process(bool $debug = false): string { if ($this->workbox->enabled === false) { return ''; @@ -26,7 +26,18 @@ public function process(): string return ''; } - $declaration = << - */ - private array $jsonOptions; - private Workbox $workbox; public function __construct( ServiceWorker $serviceWorker, private SerializerInterface $serializer, - #[Autowire('%kernel.debug%')] - bool $debug, ) { $this->workbox = $serviceWorker->workbox; - $options = [ - AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true, - AbstractObjectNormalizer::SKIP_NULL_VALUES => true, - JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR, - ]; - if ($debug === true) { - $options[JsonEncode::OPTIONS] |= JSON_PRETTY_PRINT; - } - $this->jsonOptions = $options; } - public function process(): string + public function process(bool $debug = false): string { if ($this->workbox->enabled === false || ! isset($this->workbox->offlineFallback)) { return ''; } + $options = [ 'pageFallback' => $this->workbox->offlineFallback->pageFallback, 'imageFallback' => $this->workbox->offlineFallback->imageFallback, @@ -57,13 +41,54 @@ public function process(): string if (count($options) === 0) { return ''; } - $options = count($options) === 0 ? '' : $this->serializer->serialize($options, 'json', $this->jsonOptions); + $options = count($options) === 0 ? '' : $this->serializer->serialize( + $options, + 'json', + $this->serializerOptions($debug) + ); + + $declaration = ''; + if ($debug === true) { + $declaration .= << + */ + private function serializerOptions(bool $debug): array + { + $jsonOptions = [ + AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true, + AbstractObjectNormalizer::SKIP_NULL_VALUES => true, + JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR, + ]; + if ($debug === true) { + $jsonOptions[JsonEncode::OPTIONS] |= JSON_PRETTY_PRINT; + } + + return $jsonOptions; } } diff --git a/src/ServiceWorkerRule/SkipWaiting.php b/src/ServiceWorkerRule/SkipWaiting.php index d32a155..1ef75f2 100644 --- a/src/ServiceWorkerRule/SkipWaiting.php +++ b/src/ServiceWorkerRule/SkipWaiting.php @@ -13,21 +13,42 @@ public function __construct( ) { } - public function process(): string + public function process(bool $debug = false): string { if ($this->serviceWorker->skipWaiting === false) { return ''; } - $declaration = <<manifest->widgets as $widget) { @@ -31,11 +33,19 @@ public function process(): string if (count($tags) === 0) { return ''; } - $data = $this->serializer->serialize($tags, 'json', [ - JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR, - ]); + $data = $this->serializer->serialize($tags, 'json', $this->serializerOptions($debug)); + if ($debug === true) { + $declaration = << { event.waitUntil(renderWidget(event.widget)); }); @@ -93,8 +103,35 @@ public function process(): string await self.widgets.updateByTag(widget.definition.tag, {template, data}); } } + OFFLINE_FALLBACK_STRATEGY; + if ($debug === true) { + $declaration .= << + */ + private function serializerOptions(bool $debug): array + { + $jsonOptions = [ + AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true, + AbstractObjectNormalizer::SKIP_NULL_VALUES => true, + JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR, + ]; + if ($debug === true) { + $jsonOptions[JsonEncode::OPTIONS] |= JSON_PRETTY_PRINT; + } - return trim($declaration); + return $jsonOptions; } } diff --git a/src/ServiceWorkerRule/WorkboxImport.php b/src/ServiceWorkerRule/WorkboxImport.php index 5ec58cf..c998e27 100644 --- a/src/ServiceWorkerRule/WorkboxImport.php +++ b/src/ServiceWorkerRule/WorkboxImport.php @@ -17,24 +17,59 @@ public function __construct( $this->workbox = $serviceWorker->workbox; } - public function process(): string + public function process(bool $debug = false): string { if ($this->workbox->enabled === false) { return ''; } + $declaration = ''; + if ($debug === true) { + $declaration .= <<workbox->useCDN === true) { - $declaration = <<workbox->version}/workbox-sw.js'); IMPORT_CDN_STRATEGY; } else { $publicUrl = '/' . trim($this->workbox->workboxPublicUrl, '/'); - $declaration = <<