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 4e58ffb commit 504c818
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 52 deletions.
22 changes: 0 additions & 22 deletions src/CachingStrategy/ManifestCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use const PHP_EOL;

final readonly class ManifestCache implements HasCacheStrategies
{
Expand All @@ -24,27 +23,6 @@ public function __construct(
$this->manifestPublicUrl = '/' . trim($manifestPublicUrl, '/');
}

public function process(string $body): string
{
if ($this->workbox->enabled === false) {
return $body;
}
if ($this->workbox->cacheManifest === false) {
return $body;
}

$declaration = <<<IMAGE_CACHE_RULE_STRATEGY
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);
}

public function getCacheStrategies(): array
{
return [
Expand Down
6 changes: 3 additions & 3 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@
;

/*** Service Worker Compiler Rules ***/
$container->load('SpomkyLabs\\PwaBundle\\ServiceWorkerRule\\', '../../ServiceWorkerRule/*');
$container->instanceof(ServiceWorkerRule::class)
->tag('spomky_labs_pwa.service_worker_rule')
;
$container->load('SpomkyLabs\\PwaBundle\\ServiceWorkerRule\\', '../../ServiceWorkerRule/*');

$container->load('SpomkyLabs\\PwaBundle\\CachingStrategy\\', '../../CachingStrategy/*');
$container->instanceof(HasCacheStrategies::class)
->tag('spomky_labs_pwa.cache_strategy')
;
$container->load('SpomkyLabs\\PwaBundle\\CachingStrategy\\', '../../CachingStrategy/*');

$container->load('SpomkyLabs\\PwaBundle\\MatchCallbackHandler\\', '../../MatchCallbackHandler/*');
$container->instanceof(MatchCallbackHandler::class)
->tag('spomky_labs_pwa.match_callback_handler')
;
$container->load('SpomkyLabs\\PwaBundle\\MatchCallbackHandler\\', '../../MatchCallbackHandler/*');
};
3 changes: 2 additions & 1 deletion src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ public function compile(): ?string
return null;
}
$body = '';

foreach ($this->serviceworkerRules as $rule) {
$body = $rule->process($body);
$body .= $rule->process();
}

return $body . $this->includeRootSW();
Expand Down
5 changes: 3 additions & 2 deletions src/ServiceWorkerRule/AppendCacheStrategies.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ public function __construct(
$this->jsonOptions = $options;
}

public function process(string $body): string
public function process(): string
{
$body = '';
foreach ($this->cacheStrategies as $idCacheStrategy => $cacheStrategy) {
foreach ($cacheStrategy->getCacheStrategies() as $idStrategy => $strategy) {
if ($strategy->enabled === false) {
continue;
}

$body .= PHP_EOL . PHP_EOL . trim($strategy->render(
$body .= PHP_EOL . trim($strategy->render(
sprintf('cache_%d_%d', $idCacheStrategy, $idStrategy),
$this->jsonOptions
));
Expand Down
9 changes: 4 additions & 5 deletions src/ServiceWorkerRule/ClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use const PHP_EOL;

final readonly class ClearCache implements ServiceWorkerRule
{
Expand All @@ -18,13 +17,13 @@ public function __construct(
$this->workbox = $serviceWorker->workbox;
}

public function process(string $body): string
public function process(): string
{
if ($this->workbox->enabled === false) {
return $body;
return '';
}
if ($this->workbox->clearCache === false) {
return $body;
return '';
}

$declaration = <<<CLEAR_CACHE
Expand All @@ -40,6 +39,6 @@ public function process(string $body): string
});
CLEAR_CACHE;

return $body . PHP_EOL . PHP_EOL . trim($declaration);
return trim($declaration);
}
}
9 changes: 4 additions & 5 deletions src/ServiceWorkerRule/OfflineFallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
use const PHP_EOL;

final readonly class OfflineFallback implements ServiceWorkerRule
{
Expand Down Expand Up @@ -44,10 +43,10 @@ public function __construct(
$this->jsonOptions = $options;
}

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

Expand All @@ -65,6 +64,6 @@ public function process(string $body): string
workbox.recipes.offlineFallback({$options});
OFFLINE_FALLBACK_STRATEGY;

return $body . PHP_EOL . PHP_EOL . trim($declaration);
return trim($declaration);
}
}
2 changes: 1 addition & 1 deletion src/ServiceWorkerRule/ServiceWorkerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface ServiceWorkerRule
{
public function process(string $body): string;
public function process(): string;
}
7 changes: 3 additions & 4 deletions src/ServiceWorkerRule/SkipWaiting.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace SpomkyLabs\PwaBundle\ServiceWorkerRule;

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use const PHP_EOL;

final readonly class SkipWaiting implements ServiceWorkerRule
{
Expand All @@ -14,10 +13,10 @@ public function __construct(
) {
}

public function process(string $body): string
public function process(): string
{
if ($this->serviceWorker->skipWaiting === false) {
return $body;
return '';
}

$declaration = <<<SKIP_WAITING
Expand All @@ -29,6 +28,6 @@ public function process(string $body): string
});
SKIP_WAITING;

return $body . PHP_EOL . PHP_EOL . trim($declaration);
return trim($declaration);
}
}
7 changes: 3 additions & 4 deletions src/ServiceWorkerRule/WindowsWidgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
use const PHP_EOL;

final readonly class WindowsWidgets implements ServiceWorkerRule
{
Expand All @@ -21,7 +20,7 @@ public function __construct(
) {
}

public function process(string $body): string
public function process(): string
{
$tags = [];
foreach ($this->manifest->widgets as $widget) {
Expand All @@ -30,7 +29,7 @@ public function process(string $body): string
}
}
if (count($tags) === 0) {
return $body;
return '';
}
$data = $this->serializer->serialize($tags, 'json', [
JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
Expand Down Expand Up @@ -96,6 +95,6 @@ public function process(string $body): string
}
OFFLINE_FALLBACK_STRATEGY;

return $body . PHP_EOL . PHP_EOL . trim($declaration);
return trim($declaration);
}
}
9 changes: 4 additions & 5 deletions src/ServiceWorkerRule/WorkboxImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use SpomkyLabs\PwaBundle\Dto\ServiceWorker;
use SpomkyLabs\PwaBundle\Dto\Workbox;
use const PHP_EOL;

final readonly class WorkboxImport implements ServiceWorkerRule
{
Expand All @@ -18,10 +17,10 @@ public function __construct(
$this->workbox = $serviceWorker->workbox;
}

public function process(string $body): string
public function process(): string
{
if ($this->workbox->enabled === false) {
return $body;
return '';
}
if ($this->workbox->useCDN === true) {
$declaration = <<<IMPORT_CDN_STRATEGY
Expand All @@ -35,10 +34,10 @@ public function process(string $body): string
IMPORT_CDN_STRATEGY;
}

return trim($declaration) . PHP_EOL . PHP_EOL . $body;
return trim($declaration);
}

public static function getDefaultPriority(): int
public static function getPriority(): int
{
return 1024;
}
Expand Down

0 comments on commit 504c818

Please sign in to comment.