-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better Cache Strategy Support
- Loading branch information
Showing
56 changed files
with
1,319 additions
and
889 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\CachingStrategy; | ||
|
||
use SpomkyLabs\PwaBundle\Dto\ServiceWorker; | ||
use SpomkyLabs\PwaBundle\Dto\Workbox; | ||
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin; | ||
use Symfony\Component\AssetMapper\AssetMapperInterface; | ||
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface; | ||
use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
use Symfony\Component\Serializer\Encoder\JsonEncode; | ||
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; | ||
|
||
final readonly class AssetCache implements HasCacheStrategies | ||
{ | ||
private int $jsonOptions; | ||
|
||
private string $assetPublicPrefix; | ||
|
||
private Workbox $workbox; | ||
|
||
public function __construct( | ||
ServiceWorker $serviceWorker, | ||
#[Autowire(service: 'asset_mapper.public_assets_path_resolver')] | ||
PublicAssetsPathResolverInterface $publicAssetsPathResolver, | ||
private AssetMapperInterface $assetMapper, | ||
private SerializerInterface $serializer, | ||
#[Autowire('%kernel.debug%')] | ||
bool $debug, | ||
) { | ||
$this->workbox = $serviceWorker->workbox; | ||
$this->assetPublicPrefix = rtrim($publicAssetsPathResolver->resolvePublicPath(''), '/'); | ||
$options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR; | ||
if ($debug === true) { | ||
$options |= JSON_PRETTY_PRINT; | ||
} | ||
$this->jsonOptions = $options; | ||
} | ||
|
||
public function getCacheStrategies(): array | ||
{ | ||
$urls = json_decode($this->serializer->serialize($this->getAssets(), 'json', [ | ||
JsonEncode::OPTIONS => $this->jsonOptions, | ||
]), true); | ||
return [ | ||
WorkboxCacheStrategy::create( | ||
$this->workbox->assetCache->cacheName, | ||
CacheStrategy::STRATEGY_CACHE_FIRST, | ||
sprintf("({url}) => url.pathname.startsWith('%s')", $this->assetPublicPrefix), | ||
$this->workbox->enabled && $this->workbox->assetCache->enabled, | ||
true, | ||
null, | ||
[ | ||
ExpirationPlugin::create( | ||
count($this->getAssets()) * 2, | ||
$this->workbox->assetCache->maxAgeInSeconds() ?? 60 * 60 * 24 * 365, | ||
), | ||
], | ||
$urls | ||
), | ||
]; | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
private function getAssets(): array | ||
{ | ||
$assets = []; | ||
foreach ($this->assetMapper->allAssets() as $asset) { | ||
if (preg_match($this->workbox->assetCache->regex, $asset->sourcePath) === 1) { | ||
$assets[] = $asset->publicPath; | ||
} | ||
} | ||
return $assets; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\CachingStrategy; | ||
|
||
use SpomkyLabs\PwaBundle\Dto\ServiceWorker; | ||
use SpomkyLabs\PwaBundle\Dto\Workbox; | ||
use SpomkyLabs\PwaBundle\MatchCallbackHandler\MatchCallbackHandler; | ||
use SpomkyLabs\PwaBundle\WorkboxPlugin\BackgroundSyncPlugin; | ||
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; | ||
|
||
final readonly class BackgroundSync implements HasCacheStrategies | ||
{ | ||
private Workbox $workbox; | ||
|
||
/** | ||
* @param iterable<MatchCallbackHandler> $matchCallbackHandlers | ||
*/ | ||
public function __construct( | ||
ServiceWorker $serviceWorker, | ||
#[TaggedIterator('spomky_labs_pwa.match_callback_handler')] | ||
private iterable $matchCallbackHandlers, | ||
) { | ||
$this->workbox = $serviceWorker->workbox; | ||
} | ||
|
||
/** | ||
* @return array<CacheStrategy> | ||
*/ | ||
public function getCacheStrategies(): array | ||
{ | ||
$strategies = []; | ||
foreach ($this->workbox->backgroundSync as $sync) { | ||
$strategies[] = WorkboxCacheStrategy::create( | ||
'BackgroundSync API', | ||
CacheStrategy::STRATEGY_NETWORK_ONLY, | ||
$this->prepareMatchCallback($sync->matchCallback), | ||
$this->workbox->enabled, | ||
true, | ||
null, | ||
[ | ||
BackgroundSyncPlugin::create( | ||
$sync->queueName, | ||
$sync->maxRetentionTime, | ||
$sync->forceSyncFallback, | ||
$sync->broadcastChannel | ||
), | ||
] | ||
); | ||
} | ||
|
||
return $strategies; | ||
} | ||
|
||
private function prepareMatchCallback(string $matchCallback): string | ||
{ | ||
foreach ($this->matchCallbackHandlers as $handler) { | ||
if ($handler->supports($matchCallback)) { | ||
return $handler->handle($matchCallback); | ||
} | ||
} | ||
|
||
return $matchCallback; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpomkyLabs\PwaBundle\CachingStrategy; | ||
|
||
abstract readonly class CacheStrategy | ||
{ | ||
public const STRATEGY_CACHE_FIRST = 'CacheFirst'; | ||
|
||
public const STRATEGY_CACHE_ONLY = 'CacheOnly'; | ||
|
||
public const STRATEGY_NETWORK_FIRST = 'NetworkFirst'; | ||
|
||
public const STRATEGY_NETWORK_ONLY = 'NetworkOnly'; | ||
|
||
public const STRATEGY_STALE_WHILE_REVALIDATE = 'StaleWhileRevalidate'; | ||
|
||
public const STRATEGIES = [ | ||
self::STRATEGY_CACHE_FIRST, | ||
self::STRATEGY_CACHE_ONLY, | ||
self::STRATEGY_NETWORK_FIRST, | ||
self::STRATEGY_NETWORK_ONLY, | ||
self::STRATEGY_STALE_WHILE_REVALIDATE, | ||
]; | ||
|
||
public function __construct( | ||
public string $name, | ||
public bool $enabled, | ||
public bool $requireWorkbox, | ||
) { | ||
} | ||
|
||
abstract public function render(string $cacheObjectName, int $jsonOptions = 0): string; | ||
} |
Oops, something went wrong.