Skip to content

Commit

Permalink
features: CachePlugin and CacheStrategy are now interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Mar 21, 2024
1 parent a4fd695 commit 1442a4f
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 41 deletions.
5 changes: 4 additions & 1 deletion src/CachingStrategy/WorkboxCacheStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,19 @@ public function render(string $cacheObjectName, bool $debug = false): string
}

$declaration .= <<<ROUTE_REGISTRATION
// Creation of the Workbox Cache Strategy object
const {$cacheObjectName} = new workbox.strategies.{$this->strategy}({
{$timeout}{$cacheName}plugins: {$plugins}
});
// Register the route with the Workbox Router
workbox.routing.registerRoute({$this->matchCallback},{$cacheObjectName}{$method});
ROUTE_REGISTRATION;

if ($this->preloadUrls !== []) {
$fontUrls = json_encode($this->preloadUrls, $jsonOptions);
$declaration .= <<<ASSET_CACHE_RULE_PRELOAD
// As preloading is enabled, we will add the following assets to the cache when the service worker is installed
self.addEventListener('install', event => {
const done = {$fontUrls}.map(
path =>
Expand All @@ -175,6 +177,7 @@ public function render(string $cacheObjectName, bool $debug = false): string
/**************************************************** END CACHE STRATEGY ****************************************************/
DEBUG_STATEMENT;

return $debug === true ? $declaration : trim($declaration);
Expand Down
8 changes: 7 additions & 1 deletion src/Service/ServiceWorkerCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function __construct(
private AssetMapperInterface $assetMapper,
#[TaggedIterator('spomky_labs_pwa.service_worker_rule', defaultPriorityMethod: 'getPriority')]
private iterable $serviceworkerRules,
#[Autowire('%kernel.debug%')]
public bool $debug,
) {
}

Expand All @@ -35,7 +37,11 @@ public function compile(): ?string
$body = '';

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

Check failure on line 40 in src/Service/ServiceWorkerCompiler.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Method SpomkyLabs\PwaBundle\ServiceWorkerRule\ServiceWorkerRule::process() invoked with 1 parameter, 0 required.
if ($this->debug === false) {
$ruleBody = trim($ruleBody);
}
$body .= $ruleBody;
}

return $body . $this->includeRootSW();
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceWorkerRule/AppendCacheStrategies.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
) {
}

public function process(): string
public function process(bool $debug = false): string
{
$body = '';
foreach ($this->cacheStrategies as $idCacheStrategy => $cacheStrategy) {
Expand Down
30 changes: 26 additions & 4 deletions src/ServiceWorkerRule/ClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
private Workbox $workbox;

public function __construct(
ServiceWorker $serviceWorker
ServiceWorker $serviceWorker,
) {
$this->workbox = $serviceWorker->workbox;
}

public function process(): string
public function process(bool $debug = false): string
{
if ($this->workbox->enabled === false) {
return '';
Expand All @@ -26,7 +26,18 @@ public function process(): string
return '';
}

$declaration = <<<CLEAR_CACHE
$declaration = '';
if ($debug === true) {
$declaration .= <<<DEBUG_COMMENT
/**************************************************** CACHE CLEAR ****************************************************/
// The configuration is set to clear the cache on each install event
// The following code will remove all the caches
DEBUG_COMMENT;
}

$declaration .= <<<CLEAR_CACHE
self.addEventListener("install", function (event) {
event.waitUntil(caches.keys().then(function (cacheNames) {
return Promise.all(
Expand All @@ -37,8 +48,19 @@ public function process(): string
})
);
});
CLEAR_CACHE;

return trim($declaration);
if ($debug === true) {
$declaration .= <<<DEBUG_COMMENT
/**************************************************** END CACHE CLEAR ****************************************************/
DEBUG_COMMENT;
}

return $declaration;
}
}
67 changes: 46 additions & 21 deletions src/ServiceWorkerRule/OfflineFallback.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 Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
Expand All @@ -18,36 +17,21 @@

final readonly class OfflineFallback implements ServiceWorkerRule
{
/**
* @var array<string, mixed>
*/
private array $jsonOptions;

private Workbox $workbox;

public function __construct(
ServiceWorker $serviceWorker,
private SerializerInterface $serializer,
#[Autowire('%kernel.debug%')]
bool $debug,
) {
$this->workbox = $serviceWorker->workbox;
$options = [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
];
if ($debug === true) {
$options[JsonEncode::OPTIONS] |= JSON_PRETTY_PRINT;
}
$this->jsonOptions = $options;
}

public function process(): string
public function process(bool $debug = false): string
{
if ($this->workbox->enabled === false || ! isset($this->workbox->offlineFallback)) {
return '';
}

$options = [
'pageFallback' => $this->workbox->offlineFallback->pageFallback,
'imageFallback' => $this->workbox->offlineFallback->imageFallback,
Expand All @@ -57,13 +41,54 @@ public function process(): string
if (count($options) === 0) {
return '';
}
$options = count($options) === 0 ? '' : $this->serializer->serialize($options, 'json', $this->jsonOptions);
$options = count($options) === 0 ? '' : $this->serializer->serialize(
$options,
'json',
$this->serializerOptions($debug)
);

$declaration = '';
if ($debug === true) {
$declaration .= <<<DEBUG_COMMENT
$declaration = <<<OFFLINE_FALLBACK_STRATEGY
/**************************************************** OFFLINE FALLBACK ****************************************************/
// The configuration is set to provide offline fallbacks
DEBUG_COMMENT;
}

$declaration .= <<<OFFLINE_FALLBACK_STRATEGY
workbox.routing.setDefaultHandler(new workbox.strategies.NetworkOnly());
workbox.recipes.offlineFallback({$options});
OFFLINE_FALLBACK_STRATEGY;

return trim($declaration);
if ($debug === true) {
$declaration .= <<<DEBUG_COMMENT
/**************************************************** END OFFLINE FALLBACK ****************************************************/
DEBUG_COMMENT;
}
return $declaration;
}

/**
* @return array<string, mixed>
*/
private function serializerOptions(bool $debug): array
{
$jsonOptions = [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
];
if ($debug === true) {
$jsonOptions[JsonEncode::OPTIONS] |= JSON_PRETTY_PRINT;
}

return $jsonOptions;
}
}
27 changes: 24 additions & 3 deletions src/ServiceWorkerRule/SkipWaiting.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,42 @@ public function __construct(
) {
}

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

$declaration = <<<SKIP_WAITING
$declaration = '';
if ($debug === true) {
$declaration .= <<<DEBUG_COMMENT
/**************************************************** SKIP WAITING ****************************************************/
// The configuration is set to skip waiting on each install event
DEBUG_COMMENT;
}

$declaration .= <<<SKIP_WAITING
self.addEventListener("install", function (event) {
event.waitUntil(self.skipWaiting());
});
self.addEventListener("activate", function (event) {
event.waitUntil(self.clients.claim());
});
SKIP_WAITING;
if ($debug === true) {
$declaration .= <<<DEBUG_COMMENT
/**************************************************** END SKIP WAITING ****************************************************/
DEBUG_COMMENT;
}

return trim($declaration);
return $declaration;
}
}
49 changes: 43 additions & 6 deletions src/ServiceWorkerRule/WindowsWidgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

use SpomkyLabs\PwaBundle\Dto\Manifest;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use function count;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
Expand All @@ -20,7 +22,7 @@ public function __construct(
) {
}

public function process(): string
public function process(bool $debug = false): string
{
$tags = [];
foreach ($this->manifest->widgets as $widget) {
Expand All @@ -31,11 +33,19 @@ public function process(): string
if (count($tags) === 0) {
return '';
}
$data = $this->serializer->serialize($tags, 'json', [
JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
]);
$data = $this->serializer->serialize($tags, 'json', $this->serializerOptions($debug));
if ($debug === true) {
$declaration = <<<DEBUG_COMMENT
/**************************************************** END WINDOWS WIDGETS ****************************************************/
// The following code will manage the installation and uninstallation of widgets
// NOTE: this feature is experimental and may not work as expected
DEBUG_COMMENT;
}

$declaration = <<<OFFLINE_FALLBACK_STRATEGY
$declaration .= <<<OFFLINE_FALLBACK_STRATEGY

Check failure on line 48 in src/ServiceWorkerRule/WindowsWidgets.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 Test on ubuntu-latest

Variable $declaration might not be defined.
self.addEventListener("widgetinstall", event => {
event.waitUntil(renderWidget(event.widget));
});
Expand Down Expand Up @@ -93,8 +103,35 @@ public function process(): string
await self.widgets.updateByTag(widget.definition.tag, {template, data});
}
}
OFFLINE_FALLBACK_STRATEGY;
if ($debug === true) {
$declaration .= <<<DEBUG_COMMENT
/**************************************************** END WINDOWS WIDGETS ****************************************************/
DEBUG_COMMENT;
}

return $declaration;
}

/**
* @return array<string, mixed>
*/
private function serializerOptions(bool $debug): array
{
$jsonOptions = [
AbstractObjectNormalizer::SKIP_UNINITIALIZED_VALUES => true,
AbstractObjectNormalizer::SKIP_NULL_VALUES => true,
JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
];
if ($debug === true) {
$jsonOptions[JsonEncode::OPTIONS] |= JSON_PRETTY_PRINT;
}

return trim($declaration);
return $jsonOptions;
}
}
Loading

0 comments on commit 1442a4f

Please sign in to comment.