Skip to content

Commit

Permalink
Better Cache Strategy Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 17, 2024
1 parent 9905008 commit 5ed3b83
Show file tree
Hide file tree
Showing 37 changed files with 852 additions and 483 deletions.
2 changes: 1 addition & 1 deletion assets/src/connection-status_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class extends Controller {
});
}
dispatchEvent = (name, payload) => {
this.dispatch(name, { detail: payload, prefix: 'connection-status' });
this.dispatch(name, { detail: payload });
}

statusChanged = (data) => {
Expand Down
2 changes: 1 addition & 1 deletion assets/src/sync-broadcast_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default class extends Controller {
}

dispatchEvent = (name, payload) => {
this.dispatch(name, { detail: payload, prefix: 'connection-status' });
this.dispatch(name, { detail: payload });
}

messageReceived = async (event) => {
Expand Down
38 changes: 29 additions & 9 deletions src/Command/ListCacheStrategiesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
namespace SpomkyLabs\PwaBundle\Command;

use SpomkyLabs\PwaBundle\Service\HasCacheStrategies;
use SpomkyLabs\PwaBundle\Service\Plugin\CachePlugin;
use SpomkyLabs\PwaBundle\Service\WorkboxCacheStrategy;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Symfony\Component\Yaml\Yaml;
use function count;

#[AsCommand(name: 'pwa:cache:list-strategies', description: 'List the available cache strategies',)]
final class ListCacheStrategiesCommand extends Command
Expand All @@ -32,18 +35,35 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->title('Cache Strategies');

$table = $io->createTable();
$table->setHeaders(['Name', 'Strategy', 'URL pattern', 'Enabled', 'Workbox?', 'Options']);
$table->setHeaders(
['Name', 'Strategy', 'URL pattern', 'Enabled', 'Workbox?', 'Plugins', 'Preload URLs', 'Options']
);
foreach ($this->services as $service) {
$strategies = $service->getCacheStrategies();
foreach ($strategies as $strategy) {
$table->addRow([
$strategy->name,
$strategy->strategy,
$strategy->urlPattern,
$strategy->enabled ? 'Yes' : 'No',
$strategy->requireWorkbox ? 'Yes' : 'No',
Yaml::dump($strategy->options),
]);
if ($strategy instanceof WorkboxCacheStrategy) {
$table->addRow([
$strategy->name,
$strategy->strategy,
$strategy->matchCallback,
$strategy->enabled ? 'Yes' : 'No',
$strategy->requireWorkbox ? 'Yes' : 'No',
Yaml::dump(array_map(fn (CachePlugin $v): string => $v->name, $strategy->plugins)),
count($strategy->preloadUrls),
Yaml::dump($strategy->options),
]);
} else {
$table->addRow([
$strategy->name,
$strategy->strategy,

Check failure on line 58 in src/Command/ListCacheStrategiesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Access to an undefined property SpomkyLabs\PwaBundle\Service\CacheStrategy::$strategy.
$strategy->matchCallback,

Check failure on line 59 in src/Command/ListCacheStrategiesCommand.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Access to an undefined property SpomkyLabs\PwaBundle\Service\CacheStrategy::$matchCallback.
$strategy->enabled ? 'Yes' : 'No',
$strategy->requireWorkbox ? 'Yes' : 'No',
'',
'',
'',
]);
}
}
}
$table->render();
Expand Down
10 changes: 1 addition & 9 deletions src/Dto/AssetCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@

namespace SpomkyLabs\PwaBundle\Dto;

use Symfony\Component\Serializer\Attribute\SerializedName;

final class AssetCache
final class AssetCache extends Cache
{
public bool $enabled = true;

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

public string $regex = '/\.(css|js|json|xml|txt|map|ico|png|jpe?g|gif|svg|webp|bmp)$/';

#[SerializedName('max_age')]
public int $maxAge = 60 * 60 * 24 * 365;
}
5 changes: 3 additions & 2 deletions src/Dto/BackgroundSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

use Symfony\Component\Serializer\Attribute\SerializedName;

final class BackgroundSync
final class BackgroundSync extends Cache
{
#[SerializedName('queue_name')]
public string $queueName;

public string $regex;
#[SerializedName('match_callback')]
public string $matchCallback;

Check failure on line 15 in src/Dto/BackgroundSync.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Class SpomkyLabs\PwaBundle\Dto\BackgroundSync has an uninitialized property $matchCallback. Give it default value or assign it in the constructor.

public string $method;

Expand Down
35 changes: 35 additions & 0 deletions src/Dto/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace SpomkyLabs\PwaBundle\Dto;

use DateInterval;
use DateTimeImmutable;
use Symfony\Component\Serializer\Attribute\SerializedName;
use function is_string;

abstract class Cache
{
#[SerializedName('cache_name')]
public null|string $cacheName = null;

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

#[SerializedName('max_entries')]
public null|int $maxEntries = 60;

public function maxAgeInSeconds(): null|int
{
if ($this->maxAge === null) {
return null;
}
if (is_string($this->maxAge)) {
$now = new DateTimeImmutable();
$future = $now->add(DateInterval::createFromDateString($this->maxAge));

Check failure on line 30 in src/Dto/Cache.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Parameter #1 $interval of method DateTimeImmutable::add() expects DateInterval, DateInterval|false given.
return abs($future->getTimestamp() - $now->getTimestamp());
}
return $this->maxAge;
}
}
11 changes: 1 addition & 10 deletions src/Dto/FontCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@

use Symfony\Component\Serializer\Attribute\SerializedName;

final class FontCache
final class FontCache extends Cache
{
public bool $enabled = true;

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

#[SerializedName('regex')]
public string $regex = '/\.(ttf|eot|otf|woff2)$/';

#[SerializedName('max_entries')]
public int $maxEntries = 60;

#[SerializedName('max_age')]
public int $maxAge = 60;
}
8 changes: 1 addition & 7 deletions src/Dto/GoogleFontCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@

use Symfony\Component\Serializer\Attribute\SerializedName;

final class GoogleFontCache
final class GoogleFontCache extends Cache
{
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;
}
11 changes: 1 addition & 10 deletions src/Dto/ImageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@

use Symfony\Component\Serializer\Attribute\SerializedName;

final class ImageCache
final class ImageCache extends Cache
{
public bool $enabled = true;

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

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

#[SerializedName('max_entries')]
public int $maxEntries = 60;

#[SerializedName('max_age')]
public int $maxAge = 60;
}
27 changes: 20 additions & 7 deletions src/Dto/PageCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,33 @@

use Symfony\Component\Serializer\Attribute\SerializedName;

final class PageCache
final class PageCache extends Cache
{
#[SerializedName('cache_name')]
public string $cacheName;

#[SerializedName('regex')]
public string $regex;
#[SerializedName('match_callback')]
public string $matchCallback;

Check failure on line 12 in src/Dto/PageCache.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Class SpomkyLabs\PwaBundle\Dto\PageCache has an uninitialized property $matchCallback. Give it default value or assign it in the constructor.

#[SerializedName('network_timeout')]
public int $networkTimeout = 3;

public string $strategy = 'networkFirst';
public string $strategy = 'NetworkFirst';

public bool $broadcast = false;

#[SerializedName('range_requests')]
public bool $rangeRequests = false;

/**
* @var int[]
*/
#[SerializedName('cacheable_response_statuses')]
public array $cacheableResponseStatuses = [0, 200];

/**
* @var null|array<string, string>
*/
#[SerializedName('cacheable_response_headers')]
public array $cacheableResponseHeaders = [];

Check failure on line 34 in src/Dto/PageCache.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

PHPDoc tag @var for property SpomkyLabs\PwaBundle\Dto\PageCache::$cacheableResponseHeaders with type array<string, string>|null is not subtype of native type array.

/**
* @var array<string>
*/
Expand All @@ -30,5 +42,6 @@ final class PageCache
/**
* @var array<Url>
*/
#[SerializedName('preload_urls')]
public array $urls = [];
}
Loading

0 comments on commit 5ed3b83

Please sign in to comment.