memories/src/service-worker.js

45 lines
1.6 KiB
JavaScript
Raw Normal View History

2022-12-07 20:25:19 +00:00
import { precacheAndRoute } from 'workbox-precaching';
import { NetworkFirst, CacheFirst, NetworkOnly } from 'workbox-strategies';
import { registerRoute } from 'workbox-routing';
import { ExpirationPlugin } from 'workbox-expiration';
precacheAndRoute(self.__WB_MANIFEST);
registerRoute(/^.*\/apps\/memories\/api\/video\/transcode\/.*/, new NetworkOnly());
registerRoute(/^.*\/apps\/memories\/api\/image\/jpeg\/.*/, new NetworkOnly());
registerRoute(/^.*\/remote.php\/.*/, new NetworkOnly());
registerRoute(/^.*\/apps\/files\/ajax\/download.php?.*/, new NetworkOnly());
const imageCache = new CacheFirst({
cacheName: 'images',
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 3600 * 24 * 7, // days
maxEntries: 20000, // 20k images
}),
],
});
registerRoute(/^.*\/apps\/memories\/api\/image\/preview\/.*/, imageCache);
registerRoute(/^.*\/apps\/memories\/api\/video\/livephoto\/.*/, imageCache);
registerRoute(/^.*\/apps\/memories\/api\/faces\/preview\/.*/, imageCache);
registerRoute(/^.*\/apps\/memories\/api\/tags\/preview\/.*/, imageCache);
registerRoute(/^.*\/apps\/memories\/api\/.*/, new NetworkOnly());
registerRoute(/^.*\/.*$/, new NetworkFirst({
cacheName: 'pages',
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 3600 * 24 * 7, // days
maxEntries: 2000, // assets
}),
],
}));
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());
});