2023-05-21 18:44:18 +00:00
|
|
|
import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching';
|
2023-04-19 23:14:30 +00:00
|
|
|
import { NetworkFirst, CacheFirst } from 'workbox-strategies';
|
|
|
|
import { registerRoute } from 'workbox-routing';
|
|
|
|
import { ExpirationPlugin } from 'workbox-expiration';
|
2022-12-07 20:25:19 +00:00
|
|
|
|
2023-10-19 18:52:36 +00:00
|
|
|
declare var self: ServiceWorkerGlobalScope;
|
|
|
|
|
2023-11-24 17:32:18 +00:00
|
|
|
type PrecacheEntry = Exclude<(typeof self.__WB_MANIFEST)[number], string>;
|
|
|
|
const manifest = self.__WB_MANIFEST as Array<PrecacheEntry>;
|
|
|
|
|
|
|
|
// Exclude files that are not needed
|
|
|
|
const filteredManifest = manifest.filter((entry) => {
|
|
|
|
return !entry.url?.match(/LICENSE\.txt(\?.*)?$/);
|
|
|
|
});
|
|
|
|
|
|
|
|
precacheAndRoute(filteredManifest);
|
2023-05-21 18:44:18 +00:00
|
|
|
cleanupOutdatedCaches();
|
2022-12-07 20:25:19 +00:00
|
|
|
|
2023-04-19 02:19:05 +00:00
|
|
|
registerRoute(
|
|
|
|
/^.*\/apps\/memories\/api\/video\/livephoto\/.*/,
|
|
|
|
new CacheFirst({
|
2023-05-26 16:23:22 +00:00
|
|
|
cacheName: 'memories-livephotos',
|
2023-04-19 02:19:05 +00:00
|
|
|
plugins: [
|
|
|
|
new ExpirationPlugin({
|
|
|
|
maxAgeSeconds: 3600 * 24 * 7, // days
|
|
|
|
maxEntries: 1000, // 1k videos
|
|
|
|
}),
|
|
|
|
],
|
2023-10-14 20:06:36 +00:00
|
|
|
}),
|
2023-04-19 02:19:05 +00:00
|
|
|
);
|
2022-12-07 20:25:19 +00:00
|
|
|
|
2023-04-04 00:05:44 +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
|
|
|
|
2023-05-03 06:37:11 +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({
|
2023-05-26 16:23:22 +00:00
|
|
|
cacheName: 'memories-pages',
|
2023-10-14 20:06:36 +00:00
|
|
|
}),
|
2023-05-03 06:37:11 +00:00
|
|
|
);
|
2023-04-04 00:05:44 +00:00
|
|
|
|
|
|
|
// Cache pages for same-origin requests only
|
2023-03-11 07:41:47 +00:00
|
|
|
registerRoute(
|
2023-04-19 23:14:30 +00:00
|
|
|
({ url }) => url.origin === self.location.origin && !networkOnly.some((regex) => regex.test(url.href)),
|
2023-05-03 06:26:42 +00:00
|
|
|
new CacheFirst({
|
2023-05-26 16:23:22 +00:00
|
|
|
cacheName: 'memories-pages',
|
2022-12-07 20:25:19 +00:00
|
|
|
plugins: [
|
2023-03-11 07:41:47 +00:00
|
|
|
new ExpirationPlugin({
|
|
|
|
maxAgeSeconds: 3600 * 24 * 7, // days
|
|
|
|
maxEntries: 2000, // assets
|
|
|
|
}),
|
2022-12-07 20:25:19 +00:00
|
|
|
],
|
2023-10-14 20:06:36 +00:00
|
|
|
}),
|
2023-03-11 07:41:47 +00:00
|
|
|
);
|
2022-12-07 20:33:49 +00:00
|
|
|
|
2023-04-19 23:14:30 +00:00
|
|
|
self.addEventListener('activate', (event) => {
|
2023-03-11 07:41:47 +00:00
|
|
|
// 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
|
|
|
});
|