Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple page cache #123

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 24 additions & 44 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ parameters:
count: 1
path: src/Dto/Manifest.php

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

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

-
message: "#^Class SpomkyLabs\\\\PwaBundle\\\\Dto\\\\ProtocolHandler has an uninitialized property \\$protocol\\. Give it default value or assign it in the constructor\\.$#"
count: 1
Expand Down Expand Up @@ -336,7 +346,7 @@ parameters:
path: src/Dto/Workbox.php

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

Expand Down Expand Up @@ -482,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 @@ -510,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 @@ -530,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 @@ -560,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' 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_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 @@ -660,10 +630,20 @@ parameters:
count: 1
path: src/Service/Rule/AssetCache.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\\{maxAge\\: int\\|null, maxEntries\\: int\\|null\\} given\\.$#"
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
path: src/Service/Rule/PageCache.php
path: src/Service/Rule/PageCaches.php

-
message: "#^Method SpomkyLabs\\\\PwaBundle\\\\SpomkyLabsPwaBundle\\:\\:loadExtension\\(\\) has parameter \\$config with no value type specified in iterable type array\\.$#"
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
6 changes: 2 additions & 4 deletions src/Dto/PageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

final class PageCache
{
public bool $enabled = true;

#[SerializedName('cache_name')]
public string $cacheName = 'pages';
public string $cacheName;

#[SerializedName('regex')]
public string $regex = '/\.(ico|png|jpe?g|gif|svg|webp|bmp)$/';
public string $regex;

#[SerializedName('network_timeout')]
public int $networkTimeout = 3;
Expand Down
7 changes: 5 additions & 2 deletions src/Dto/Workbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ final class Workbox
#[SerializedName('font_cache')]
public FontCache $fontCache;

#[SerializedName('page_cache')]
public PageCache $pageCache;
/**
* @var array<PageCache>
*/
#[SerializedName('page_caches')]
public array $pageCaches;

#[SerializedName('asset_cache')]
public AssetCache $assetCache;
Expand Down
166 changes: 73 additions & 93 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_cache'])) {
return $v;
}
$v['page_cache'] = array_filter([
'enabled' => true,
'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 Expand Up @@ -281,64 +251,74 @@
->end()
->end()
->end()
->arrayNode('page_cache')
->canBeDisabled()
->children()
->scalarNode('cache_name')
->defaultValue('pages')
->info('The name of the page cache.')
->end()
->integerNode('network_timeout')
->defaultValue(3)
->info(
'The network timeout in seconds before cache is called (for warm cache URLs only).'
)
->example([1, 2, 5])
->end()
->scalarNode('strategy')
->defaultValue('networkFirst')
->info(
'The caching strategy. Only "networkFirst" and "staleWhileRevalidate" are supported.'
)
->example(['networkFirst', 'staleWhileRevalidate'])
->end()
->booleanNode('broadcast')
->defaultFalse()
->info(
'Whether to broadcast the cache update events. Only supported with "staleWhileRevalidate" strategy.'
)
->end()
->arrayNode('broadcast_headers')
->treatNullLike(['Content-Length', 'ETag', 'Last-Modified'])
->treatFalseLike(['Content-Length', 'ETag', 'Last-Modified'])
->treatTrueLike(['Content-Length', 'ETag', 'Last-Modified'])
->defaultValue(['Content-Length', 'ETag', 'Last-Modified'])
->scalarPrototype()->end()
->end()
->arrayNode('urls')
->treatNullLike([])
->treatFalseLike([])
->treatTrueLike([])
->info('The URLs to warm the cache. The URLs shall be served by the application.')
->arrayPrototype()
->beforeNormalization()
->ifString()
->then(static fn (string $v): array => [
'path' => $v,
])
->end()
->children()
->scalarNode('path')
->isRequired()
->info('The URL of the shortcut.')
->example('app_homepage')
->arrayNode('page_caches')
->treatNullLike([])
->treatFalseLike([])
->treatTrueLike([])
->arrayPrototype()
->children()
->scalarNode('regex')
->isRequired()
->info('The regex to match the URLs.')
->end()
->scalarNode('cache_name')
->isRequired()
->info('The name of the page cache.')
->end()
->integerNode('network_timeout')
->defaultValue(3)
->info(
'The network timeout in seconds before cache is called (for "networkFirst" strategy only).'
)
->example([1, 2, 5])
->end()
->scalarNode('strategy')
->defaultValue('networkFirst')
->info(
'The caching strategy. Only "networkFirst" and "staleWhileRevalidate" are supported.'
)
->example(['networkFirst', 'staleWhileRevalidate'])
->end()
->booleanNode('broadcast')
->defaultFalse()
->info(
'Whether to broadcast the cache update events (for "staleWhileRevalidate" strategy only).'
)
->end()
->arrayNode('broadcast_headers')
->treatNullLike(['Content-Length', 'ETag', 'Last-Modified'])
->treatFalseLike(['Content-Length', 'ETag', 'Last-Modified'])
->treatTrueLike(['Content-Length', 'ETag', 'Last-Modified'])
->defaultValue(['Content-Length', 'ETag', 'Last-Modified'])
->scalarPrototype()->end()
->end()
->arrayNode('urls')
->treatNullLike([])
->treatFalseLike([])
->treatTrueLike([])
->info(
'The URLs to warm the cache. The URLs shall be served by the application.'
)
->arrayPrototype()
->beforeNormalization()
->ifString()
->then(static fn (string $v): array => [
'path' => $v,
])
->end()
->arrayNode('params')
->treatFalseLike([])
->treatTrueLike([])
->treatNullLike([])
->prototype('variable')->end()
->info('The parameters of the action.')
->children()
->scalarNode('path')
->isRequired()
->info('The URL of the shortcut.')
->example('app_homepage')
->end()
->arrayNode('params')
->treatFalseLike([])
->treatTrueLike([])
->treatNullLike([])
->prototype('variable')->end()
->info('The parameters of the action.')
->end()
->end()
->end()
->end()
Expand Down Expand Up @@ -409,7 +389,7 @@
->setDeprecated(
'spomky-labs/phpwa',
'1.1.0',
'The "%node%" option is deprecated and will be removed in 2.0.0. Please use "pwa.serviceworker.workbox.page_cache.cache_name" instead.'
'The "%node%" option is deprecated and will be removed in 2.0.0. Please use "pwa.serviceworker.workbox.page_caches[].cache_name" instead.'
)
->end()
->scalarNode('asset_cache_name')
Expand Down Expand Up @@ -501,7 +481,7 @@
->setDeprecated(
'spomky-labs/phpwa',
'1.1.0',
'The "%node%" option is deprecated and will be removed in 2.0.0. Please use "pwa.serviceworker.workbox.page_cache.network_timeout" instead.'
'The "%node%" option is deprecated and will be removed in 2.0.0. Please use "pwa.serviceworker.workbox.page_caches[].network_timeout" instead.'
)
->end()
->arrayNode('warm_cache_urls')
Expand All @@ -512,7 +492,7 @@
->setDeprecated(
'spomky-labs/phpwa',
'1.1.0',
'The "%node%" option is deprecated and will be removed in 2.0.0. Please use "pwa.serviceworker.workbox.page_cache.urls" instead.'
'The "%node%" option is deprecated and will be removed in 2.0.0. Please use "pwa.serviceworker.workbox.page_caches[].urls" instead.'
)
->arrayPrototype()
->beforeNormalization()
Expand Down
5 changes: 1 addition & 4 deletions src/Service/Rule/BackgroundSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,12 @@ public function getCacheStrategies(): array
$strategies = [];
foreach ($this->workbox->backgroundSync as $sync) {
$strategies[] = CacheStrategy::create(
'---',
'BackgroundSync API',
CacheStrategy::STRATEGY_NETWORK_ONLY,
$sync->regex,
$this->workbox->enabled,
true,
[
'maxTimeout' => 0,
'maxAge' => 0,
'maxEntries' => 0,
'plugins' => [
sprintf('backgroundSync: "%s"', $sync->queueName),
sprintf('broadcastChannel: "%s"', $sync->broadcastChannel ?? '---'),
Expand Down
Loading