Skip to content

Commit

Permalink
Cache site manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 5, 2024
1 parent 273cb82 commit a3d9a8a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/Dto/Workbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/definition/service_worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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."')
Expand Down
25 changes: 25 additions & 0 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 = <<<IMAGE_CACHE_RULE_STRATEGY
//Cache manifest file
workbox.routing.registerRoute(
({url}) => '{$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) {
Expand Down

0 comments on commit a3d9a8a

Please sign in to comment.