Custom Cache Strategy
When to Use Custom Strategies
Quick Start: Using WorkboxCacheStrategy
<?php
declare(strict_types=1);
namespace App\Service;
use SpomkyLabs\PwaBundle\CachingStrategy\CacheStrategyInterface;
use SpomkyLabs\PwaBundle\CachingStrategy\HasCacheStrategiesInterface;
use SpomkyLabs\PwaBundle\CachingStrategy\WorkboxCacheStrategy;
use SpomkyLabs\PwaBundle\WorkboxPlugin\ExpirationPlugin;
final readonly class MyCustomCacheStrategies implements HasCacheStrategiesInterface
{
/**
* @return array<CacheStrategyInterface>
*/
public function getCacheStrategies(): array
{
return [
WorkboxCacheStrategy::create(
enabled: true,
requireWorkbox: true,
strategy: CacheStrategyInterface::STRATEGY_CACHE_FIRST,
matchCallback: '({url}) => url.pathname.startsWith("/api/static/")',
)
->withName('api-static-data')
->withPlugin(
ExpirationPlugin::create(
maxEntries: 50,
maxAgeSeconds: 86400, // 1 day
)
),
];
}
}Complete Example: Multiple Strategies
WorkboxCacheStrategy API
create()
Fluent Methods
Method
Description
Available Workbox Plugins
ExpirationPlugin
CacheableResponsePlugin
BroadcastUpdatePlugin
BackgroundSyncPlugin
RangeRequestsPlugin
Match Callback Patterns
Path Prefix
Regular Expression
Origin Match
Custom JavaScript Callback
Navigation Requests
Conditional Strategies
Implementing CacheStrategyInterface Directly
Service Registration
Listing Cache Strategies
Debugging
Check Generated Service Worker
Chrome DevTools
Best Practices
Related Documentation
Last updated
Was this helpful?