memories/src/service-worker.ts

58 lines
1.7 KiB
TypeScript
Raw Normal View History

import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching';
import { NetworkFirst, CacheFirst } from 'workbox-strategies';
import { registerRoute } from 'workbox-routing';
import { ExpirationPlugin } from 'workbox-expiration';
2022-12-07 20:25:19 +00:00
declare var self: ServiceWorkerGlobalScope;
2022-12-07 20:25:19 +00:00
precacheAndRoute(self.__WB_MANIFEST);
cleanupOutdatedCaches();
2022-12-07 20:25:19 +00:00
registerRoute(
/^.*\/apps\/memories\/api\/video\/livephoto\/.*/,
new CacheFirst({
cacheName: 'memories-livephotos',
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 3600 * 24 * 7, // days
maxEntries: 1000, // 1k videos
}),
],
}),
);
2022-12-07 20:25:19 +00:00
// Important: Using the NetworkOnly strategy and not registering
// a route are NOT equivalent. The NetworkOnly strategy will
// strip certain headers such as HTTP-Range, which is required
// for proper playback of videos.
2022-12-07 20:25:19 +00:00
const networkOnly = [/^.*\/api\/.*/];
// Use network-first for memories page for initial state such as theming
registerRoute(
({ url }) => url.origin === self.location.origin && url.pathname.endsWith('/apps/memories/'),
new NetworkFirst({
cacheName: 'memories-pages',
}),
);
// Cache pages for same-origin requests only
registerRoute(
({ url }) => url.origin === self.location.origin && !networkOnly.some((regex) => regex.test(url.href)),
new CacheFirst({
cacheName: 'memories-pages',
2022-12-07 20:25:19 +00:00
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 3600 * 24 * 7, // days
maxEntries: 2000, // assets
}),
2022-12-07 20:25:19 +00:00
],
}),
);
2022-12-07 20:33:49 +00:00
self.addEventListener('activate', (event) => {
// Take control of all pages under this SW's scope immediately,
// instead of waiting for reload/navigation.
event.waitUntil(self.clients.claim());
2022-12-07 20:33:49 +00:00
});