Skip to content

Commit

Permalink
Allow Google Font cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 5, 2024
1 parent 18d0df2 commit 2f15eb0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
10 changes: 10 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ parameters:
count: 1
path: src/Dto/FileHandler.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\GoogleFontCache has an uninitialized property \\$enabled\\. Give it default value or assign it in the constructor\\.$#"
count: 1
path: src/Dto/GoogleFontCache.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Icon has an uninitialized property \\$sizeList\\. Give it default value or assign it in the constructor\\.$#"
count: 1
Expand Down Expand Up @@ -300,6 +305,11 @@ parameters:
count: 1
path: src/Dto/Workbox.php

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\Workbox has an uninitialized property \\$googleFontCache\\. 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 \\$offlineFallbackPlaceholder\\. Give it default value or assign it in the constructor\\.$#"
count: 1
Expand Down
21 changes: 21 additions & 0 deletions src/Dto/GoogleFontCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Dto;

use Symfony\Component\Serializer\Attribute\SerializedName;

final class GoogleFontCache
{
public bool $enabled;

#[SerializedName('cache_prefix')]
public null|string $cachePrefix = null;

#[SerializedName('max_age')]
public null|int $maxAge = null;

#[SerializedName('max_entries')]
public null|int $maxEntries = null;
}
3 changes: 3 additions & 0 deletions src/Dto/Workbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ final class Workbox
#[SerializedName('cache_manifest')]
public bool $cacheManifest;

#[SerializedName('google_fonts')]
public GoogleFontCache $googleFontCache;

#[SerializedName('workbox_import_placeholder')]
#[Deprecated('No longer used.')]
public string $workboxImportPlaceholder;
Expand Down
21 changes: 21 additions & 0 deletions src/Resources/config/definition/service_worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,31 @@
->defaultFalse()
->info('Whether to use the local workbox or the CDN.')
->end()
->arrayNode('google_fonts')
->canBeDisabled()
->children()
->scalarNode('cache_prefix')
->defaultNull()
->info('The cache prefix for the Google fonts.')
->end()
->integerNode('max_age')
->defaultNull()
->info('The maximum age of the Google fonts cache (in seconds).')
->end()
->integerNode('max_entries')
->defaultNull()
->info('The maximum number of entries in the Google fonts cache.')
->end()
->end()
->end()
->booleanNode('cache_manifest')
->defaultTrue()
->info('Whether to cache the manifest file.')
->end()
->booleanNode('cache_google_fonts')
->defaultTrue()
->info('Whether to cache the Google fonts.')
->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
22 changes: 22 additions & 0 deletions src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private function processWorkbox(Workbox $workbox, string $body): string
$body = $this->processPageImageCacheRule($workbox, $body);
$body = $this->processImageCacheRule($workbox, $body);
$body = $this->processCacheRootFilesRule($workbox, $body);
$body = $this->processCacheGoogleFontsRule($workbox, $body);

return $this->processOfflineFallback($workbox, $body);
}
Expand Down Expand Up @@ -285,6 +286,27 @@ private function processCacheRootFilesRule(Workbox $workbox, string $body): stri
return $body . PHP_EOL . PHP_EOL . trim($declaration);
}

private function processCacheGoogleFontsRule(Workbox $workbox, string $body): string
{
if ($workbox->googleFontCache->enabled === false) {
return $body;
}
$options = [
'cachePrefix' => $workbox->googleFontCache->cachePrefix,
'maxAge' => $workbox->googleFontCache->maxAge,
'maxEntries' => $workbox->googleFontCache->maxEntries,
];
$options = array_filter($options, static fn (mixed $v): bool => ($v !== null && $v !== ''));
$options = count($options) === 0 ? '' : $this->serializer->serialize($options, 'json', $this->jsonOptions);

$declaration = <<<IMAGE_CACHE_RULE_STRATEGY
//Cache Google Fonts
workbox.recipes.googleFontsCache({$options});
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 2f15eb0

Please sign in to comment.