Skip to content

Commit

Permalink
Allow multiple page cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 9, 2024
1 parent 1337b75 commit 0f74e43
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 83 deletions.
49 changes: 7 additions & 42 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ parameters:

-
message: "#^Anonymous function should return array but returns mixed\\.$#"
count: 10
count: 6
path: src/Resources/config/definition/service_worker.php

-
Expand Down Expand Up @@ -520,11 +520,6 @@ parameters:
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'font_fallback' on mixed\\.$#"
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'font_regex' on mixed\\.$#"
count: 1
Expand All @@ -540,11 +535,6 @@ parameters:
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'image_fallback' on mixed\\.$#"
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'image_regex' on mixed\\.$#"
count: 1
Expand All @@ -570,44 +560,14 @@ parameters:
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'network_timeout…' on mixed\\.$#"
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'offline_fallback' on mixed\\.$#"
count: 2
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'page_cache_name' on mixed\\.$#"
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'page_caches' on mixed\\.$#"
count: 2
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'page_fallback' on mixed\\.$#"
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'static_regex' on mixed\\.$#"
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Cannot access offset 'warm_cache_urls' on mixed\\.$#"
count: 1
path: src/Resources/config/definition/service_worker.php

-
message: "#^Strict comparison using \\!\\=\\= between mixed and null will always evaluate to true\\.$#"
count: 4
count: 3
path: src/Resources/config/definition/service_worker.php

-
Expand Down Expand Up @@ -675,6 +635,11 @@ parameters:
count: 1
path: src/Service/Rule/GoogleFontCache.php

-
message: "#^Strict comparison using \\=\\=\\= between int\\<1, max\\> and 0 will always evaluate to false\\.$#"
count: 1
path: src/Service/Rule/OfflineFallback.php

-
message: "#^Parameter \\#6 \\$options of static method SpomkyLabs\\\\PwaBundle\\\\Service\\\\CacheStrategy\\:\\:create\\(\\) expects array\\{maxTimeout\\?\\: int, maxAge\\?\\: int, maxEntries\\?\\: int, warmUrls\\?\\: array\\<string\\>, plugins\\?\\: array\\<string\\>\\}, array\\{maxTimeout\\: int, plugins\\: array\\{0\\: 'CacheableResponsePl…', 1\\?\\: 'BroadcastUpdatePlug…'\\}, warmUrls\\: mixed\\} given\\.$#"
count: 1
Expand Down
2 changes: 0 additions & 2 deletions src/Dto/OfflineFallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

final class OfflineFallback
{
public bool $enabled = true;

#[SerializedName('page')]
public null|Url $pageFallback = null;

Expand Down
36 changes: 3 additions & 33 deletions src/Resources/config/definition/service_worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,38 +94,6 @@
return $v;
})
->end()
->beforeNormalization()
->ifTrue(static fn (mixed $v): bool => true)
->then(static function (mixed $v): array {
if (isset($v['page_caches'])) {
return $v;
}
$v['page_caches'] = [];
$v['page_caches'][] = array_filter([
'cache_name' => $v['page_cache_name'] ?? 'pages',
'network_timeout' => $v['network_timeout_seconds'] ?? 3,
'urls' => $v['warm_cache_urls'] ?? [],
], static fn (mixed $v): bool => $v !== null);

return $v;
})
->end()
->beforeNormalization()
->ifTrue(static fn (mixed $v): bool => true)
->then(static function (mixed $v): array {
if (isset($v['offline_fallback'])) {
return $v;
}
$v['offline_fallback'] = array_filter([
'enabled' => true,
'page' => $v['page_fallback'] ?? null,
'image' => $v['image_fallback'] ?? null,
'font' => $v['font_fallback'] ?? null,
], static fn (mixed $v): bool => $v !== null);

return $v;
})
->end()
->children()
->booleanNode('use_cdn')
->defaultFalse()
Expand Down Expand Up @@ -207,7 +175,9 @@
->info('Whether to clear the cache during the service worker activation.')
->end()
->arrayNode('offline_fallback')
->canBeDisabled()
->treatNullLike([])
->treatFalseLike([])
->treatTrueLike([])
->children()
->append(getUrlNode('page', 'The URL of the offline page fallback.'))
->append(getUrlNode('image', 'The URL of the offline image fallback.'))
Expand Down
4 changes: 2 additions & 2 deletions src/Service/Rule/AssetCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function process(string $body): string
$assetUrlsLength = count($assets) * 2;

$declaration = <<<ASSET_CACHE_RULE_STRATEGY
const assetCacheStrategy = new workbox.strategies.CacheOnly({
const assetCacheStrategy = new workbox.strategies.CacheFirst({
cacheName: '{$this->workbox->assetCache->cacheName}',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({statuses: [0, 200]}),
Expand Down Expand Up @@ -103,7 +103,7 @@ public function getCacheStrategies(): array
return [
CacheStrategy::create(
$this->workbox->assetCache->cacheName,
CacheStrategy::STRATEGY_CACHE_ONLY,
CacheStrategy::STRATEGY_CACHE_FIRST,
sprintf("'({url}) => url.pathname.startsWith('%s')'", $this->assetPublicPrefix),
$this->workbox->enabled && $this->workbox->assetCache->enabled,
true,
Expand Down
8 changes: 4 additions & 4 deletions src/Service/Rule/OfflineFallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ public function __construct(

public function process(string $body): string
{
if ($this->workbox->enabled === false) {
return $body;
}
if ($this->workbox->offlineFallback->enabled === false) {
if ($this->workbox->enabled === false || ! isset($this->workbox->offlineFallback)) {
return $body;
}
$options = [
Expand All @@ -58,6 +55,9 @@ public function process(string $body): string
'fontFallback' => $this->workbox->offlineFallback->fontFallback,
];
$options = array_filter($options, static fn (mixed $v): bool => $v !== null);
if (count($options) === 0) {
return $body;
}
$options = count($options) === 0 ? '' : $this->serializer->serialize($options, 'json', $this->jsonOptions);

$declaration = <<<OFFLINE_FALLBACK_STRATEGY
Expand Down

0 comments on commit 0f74e43

Please sign in to comment.