diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 5a26005..b036f21 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -146,6 +146,26 @@ private function setupServiceWorker(ArrayNodeDefinition $node): void ) ->example('//WIDGETS_PLACEHOLDER') ->end() + ->booleanNode('clear_cache') + ->defaultTrue() + ->info('Whether to clear the cache during the service worker activation.') + ->end() + ->scalarNode('image_cache_name') + ->defaultValue('images') + ->info('The name of the image cache.') + ->end() + ->scalarNode('font_cache_name') + ->defaultValue('fonts') + ->info('The name of the font cache.') + ->end() + ->scalarNode('page_cache_name') + ->defaultValue('pages') + ->info('The name of the page cache.') + ->end() + ->scalarNode('asset_cache_name') + ->defaultValue('assets') + ->info('The name of the asset cache.') + ->end() ->append($this->getUrlNode('page_fallback', 'The URL of the offline page fallback.')) ->append($this->getUrlNode('image_fallback', 'The URL of the offline image fallback.')) ->append($this->getUrlNode('font_fallback', 'The URL of the offline font fallback.')) diff --git a/src/Dto/Workbox.php b/src/Dto/Workbox.php index 9d70230..0308428 100644 --- a/src/Dto/Workbox.php +++ b/src/Dto/Workbox.php @@ -64,5 +64,20 @@ final class Workbox public string $imageRegex = '/\.(ico|png|jpe?g|gif|svg|webp|bmp)$/'; #[SerializedName('static_regex')] - public string $staticRegex = '/\.(css|js|json|xml|txt|woff2|ttf|eot|otf|map|webmanifest)$/'; + public string $staticRegex = '/\.(css|m?jsx?|json|xml|txt|woff2|ttf|eot|otf|map|webmanifest)$/'; + + #[SerializedName('clear_cache')] + public bool $clearCache = true; + + #[SerializedName('image_cache_name')] + public string $imageCacheName = 'images'; + + #[SerializedName('font_cache_name')] + public string $fontCacheName = 'fonts'; + + #[SerializedName('page_cache_name')] + public string $pageCacheName = 'pages'; + + #[SerializedName('asset_cache_name')] + public string $assetCacheName = 'assets'; } diff --git a/src/Service/ServiceWorkerCompiler.php b/src/Service/ServiceWorkerCompiler.php index 5d4bf5d..e482c08 100644 --- a/src/Service/ServiceWorkerCompiler.php +++ b/src/Service/ServiceWorkerCompiler.php @@ -75,6 +75,7 @@ private function processSkipWaiting(string $body): string private function processWorkbox(Workbox $workbox, string $body): string { $body = $this->processWorkboxImport($workbox, $body); + $body = $this->processClearCache($workbox, $body); $body = $this->processStandardRules($workbox, $body); $body = $this->processWidgets($workbox, $body); @@ -101,6 +102,28 @@ private function processWorkboxImport(Workbox $workbox, string $body): string return str_replace($workbox->workboxImportPlaceholder, trim($declaration), $body); } + private function processClearCache(Workbox $workbox, string $body): string + { + if ($workbox->clearCache === false) { + return $body; + } + + $declaration = <<standardRulesPlaceholder)) { @@ -125,24 +148,24 @@ private function processStandardRules(Workbox $workbox, string $body): string $declaration = <<pageCacheName}', networkTimeoutSeconds: {$workbox->networkTimeoutSeconds}, warmCache: {$routes} }); workbox.recipes.imageCache({ - cacheName: 'images', + cacheName: '{$workbox->imageCacheName}', maxEntries: {$workbox->maxImageCacheEntries}, maxImageAge: {$workbox->maxImageAge}, warmCache: {$imageUrls} }); workbox.recipes.staticResourceCache({ - cacheName: 'assets', + cacheName: '{$workbox->assetCacheName}', warmCache: {$staticUrls} }); workbox.routing.registerRoute( ({request}) => request.destination === 'font', new workbox.strategies.CacheFirst({ - cacheName: 'fonts', + cacheName: '{$workbox->fontCacheName}', plugins: [ new workbox.cacheableResponse.CacheableResponsePlugin({ statuses: [0, 200],