cache emojis with service worker
This commit is contained in:
@ -326,6 +326,43 @@ const handleNotificationClick = (event: NotificationEvent) => {
|
||||
event.waitUntil(reactToNotificationClick);
|
||||
};
|
||||
|
||||
// --- Asset caching ---
|
||||
|
||||
const CACHE_NAME = 'ncd-assets-v1';
|
||||
|
||||
/** URL patterns to cache with a cache-first strategy. */
|
||||
const CACHEABLE_PATTERNS = [
|
||||
/\/packs\/emoji\/.*\.svg$/, // Built-in emoji SVGs
|
||||
/\/packs\/assets\/.*\.woff2?$/, // Font files
|
||||
/\/packs\/assets\/.*\.css$/, // Stylesheets
|
||||
/\/emoji\/.*\.(png|svg|gif|webp)$/, // Custom instance emojis
|
||||
];
|
||||
|
||||
const isCacheable = (url: string): boolean =>
|
||||
CACHEABLE_PATTERNS.some((pattern) => pattern.test(url));
|
||||
|
||||
/** Cache-first fetch handler for static assets. */
|
||||
const handleFetch = (event: FetchEvent) => {
|
||||
const { request } = event;
|
||||
|
||||
if (request.method !== 'GET' || !isCacheable(request.url)) return;
|
||||
|
||||
event.respondWith(
|
||||
caches.match(request).then((cached) => {
|
||||
if (cached) return cached;
|
||||
|
||||
return fetch(request).then((response) => {
|
||||
if (response.ok) {
|
||||
const clone = response.clone();
|
||||
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
|
||||
}
|
||||
return response;
|
||||
});
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
// ServiceWorker event listeners
|
||||
self.addEventListener('push', handlePush);
|
||||
self.addEventListener('notificationclick', handleNotificationClick);
|
||||
self.addEventListener('fetch', handleFetch);
|
||||
|
||||
Reference in New Issue
Block a user