Skip to content

Commit

Permalink
[SW] Purge cache on install
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Feb 9, 2024
1 parent 9720ee5 commit f48f070
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
20 changes: 20 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.'))
Expand Down
17 changes: 16 additions & 1 deletion src/Dto/Workbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
31 changes: 27 additions & 4 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 = <<<CLEAR_CACHE
self.addEventListener("install", function (event) {
event.waitUntil(caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
CLEAR_CACHE;

return $body . trim($declaration);
}

private function processStandardRules(Workbox $workbox, string $body): string
{
if (! str_contains($body, $workbox->standardRulesPlaceholder)) {
Expand All @@ -125,24 +148,24 @@ private function processStandardRules(Workbox $workbox, string $body): string

$declaration = <<<STANDARD_RULE_STRATEGY
workbox.recipes.pageCache({
cacheName: 'pages',
cacheName: '{$workbox->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],
Expand Down

0 comments on commit f48f070

Please sign in to comment.