From a3d9a8a66ed4332c17b1f1be861a52b13cefcb19 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Tue, 5 Mar 2024 13:45:38 +0100 Subject: [PATCH] Cache site manifest --- phpstan-baseline.neon | 5 ++++ src/Dto/Workbox.php | 3 +++ .../config/definition/service_worker.php | 4 +++ src/Service/ServiceWorkerCompiler.php | 25 +++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f786269..0b9b57d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -290,6 +290,11 @@ parameters: count: 4 path: src/Dto/Workbox.php + - + message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$cacheManifest\\. Give it default value or assign it in the constructor\\.$#" + count: 1 + path: src/Dto/Workbox.php + - message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$enabled\\. Give it default value or assign it in the constructor\\.$#" count: 1 diff --git a/src/Dto/Workbox.php b/src/Dto/Workbox.php index c034d78..28f05c1 100644 --- a/src/Dto/Workbox.php +++ b/src/Dto/Workbox.php @@ -19,6 +19,9 @@ final class Workbox #[SerializedName('workbox_public_url')] public string $workboxPublicUrl; + #[SerializedName('cache_manifest')] + public bool $cacheManifest; + #[SerializedName('workbox_import_placeholder')] #[Deprecated('No longer used.')] public string $workboxImportPlaceholder; diff --git a/src/Resources/config/definition/service_worker.php b/src/Resources/config/definition/service_worker.php index c16e94a..9ead061 100644 --- a/src/Resources/config/definition/service_worker.php +++ b/src/Resources/config/definition/service_worker.php @@ -40,6 +40,10 @@ ->defaultFalse() ->info('Whether to use the local workbox or the CDN.') ->end() + ->booleanNode('cache_manifest') + ->defaultTrue() + ->info('Whether to cache the manifest file.') + ->end() ->scalarNode('version') ->defaultValue('7.0.0') ->info('The version of workbox. When using local files, the version shall be "7.0.0."') diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index 08c8d9d..3dd0933 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -24,16 +24,21 @@ { private array $jsonOptions; + private string $manifestPublicUrl; + public function __construct( private SerializerInterface $serializer, #[Autowire('%spomky_labs_pwa.asset_public_prefix%')] private readonly string $assetPublicPrefix, + #[Autowire('%spomky_labs_pwa.manifest.public_url%')] + string $manifestPublicUrl, #[Autowire('%spomky_labs_pwa.sw.enabled%')] private bool $serviceWorkerEnabled, private Manifest $manifest, private ServiceWorker $serviceWorker, private AssetMapperInterface $assetMapper, ) { + $this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/'); $this->jsonOptions = [ JsonEncode::OPTIONS => JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR, ]; @@ -89,6 +94,7 @@ private function processWorkbox(Workbox $workbox, string $body): string $body = $this->processFontCacheRules($workbox, $body); $body = $this->processPageImageCacheRule($workbox, $body); $body = $this->processImageCacheRule($workbox, $body); + $body = $this->processCacheRootFilesRule($workbox, $body); return $this->processOfflineFallback($workbox, $body); } @@ -260,6 +266,25 @@ private function processImageCacheRule(Workbox $workbox, string $body): string return $body . PHP_EOL . PHP_EOL . trim($declaration); } + private function processCacheRootFilesRule(Workbox $workbox, string $body): string + { + if ($workbox->cacheManifest === false) { + return $body; + } + + $declaration = << '{$this->manifestPublicUrl}' === url.pathname, + new workbox.strategies.StaleWhileRevalidate({ + cacheName: 'manifest' + }) +); +IMAGE_CACHE_RULE_STRATEGY; + + return $body . PHP_EOL . PHP_EOL . trim($declaration); + } + private function processOfflineFallback(Workbox $workbox, string $body): string { if ($workbox->pageFallback === null && $workbox->imageFallback === null && $workbox->fontFallback === null) {