Custom Cache Strategy
Overview
When to Use Custom Cache Strategies
Quick Example
<?php
namespace App\ServiceWorker\CacheStrategy;
use SpomkyLabs\PwaBundle\CachingStrategy\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
#[AsTaggedItem('spomky_labs_pwa.cache_strategy')]
final readonly class ApiCacheStrategy extends WorkboxCacheStrategy
{
public static function create(): self
{
return WorkboxCacheStrategy::create(
enabled: true,
requireWorkbox: true,
strategy: CacheStrategyInterface::STRATEGY_NETWORK_FIRST,
matchCallback: '({url}) => url.pathname.startsWith("/api/")',
)
->withName('api-cache')
->withPlugin(
ExpirationPlugin::create(
maxEntries: 50,
maxAgeSeconds: 3600,
)
);
}
}Complete Documentation
Standard Workbox Configuration
Related Documentation
Resources
Last updated
Was this helpful?