sw: prevent caching map requests

Signed-off-by: Varun Patil <varunpatil@ucla.edu>
pull/474/head
Varun Patil 2023-03-10 23:41:47 -08:00
parent d0c10b5333
commit 9b2af90546
1 changed files with 34 additions and 24 deletions

View File

@ -1,18 +1,24 @@
import { precacheAndRoute } from 'workbox-precaching'; import { precacheAndRoute } from "workbox-precaching";
import { NetworkFirst, CacheFirst, NetworkOnly } from 'workbox-strategies'; import { NetworkFirst, CacheFirst, NetworkOnly } from "workbox-strategies";
import { registerRoute } from 'workbox-routing'; import { registerRoute } from "workbox-routing";
import { ExpirationPlugin } from 'workbox-expiration'; import { ExpirationPlugin } from "workbox-expiration";
precacheAndRoute(self.__WB_MANIFEST); precacheAndRoute(self.__WB_MANIFEST);
registerRoute(/^.*\/apps\/memories\/api\/video\/transcode\/.*/, new NetworkOnly()); registerRoute(
/^.*\/apps\/memories\/api\/video\/transcode\/.*/,
new NetworkOnly()
);
registerRoute(/^.*\/apps\/memories\/api\/image\/jpeg\/.*/, new NetworkOnly()); registerRoute(/^.*\/apps\/memories\/api\/image\/jpeg\/.*/, new NetworkOnly());
registerRoute(/^.*\/apps\/memories\/api\/image\/preview\/.*/, new NetworkOnly()); registerRoute(
/^.*\/apps\/memories\/api\/image\/preview\/.*/,
new NetworkOnly()
);
registerRoute(/^.*\/remote.php\/.*/, new NetworkOnly()); registerRoute(/^.*\/remote.php\/.*/, new NetworkOnly());
registerRoute(/^.*\/apps\/files\/ajax\/download.php?.*/, new NetworkOnly()); registerRoute(/^.*\/apps\/files\/ajax\/download.php?.*/, new NetworkOnly());
const imageCache = new CacheFirst({ const imageCache = new CacheFirst({
cacheName: 'images', cacheName: "images",
plugins: [ plugins: [
new ExpirationPlugin({ new ExpirationPlugin({
maxAgeSeconds: 3600 * 24 * 7, // days maxAgeSeconds: 3600 * 24 * 7, // days
@ -27,17 +33,21 @@ registerRoute(/^.*\/apps\/memories\/api\/tags\/preview\/.*/, imageCache);
registerRoute(/^.*\/apps\/memories\/api\/.*/, new NetworkOnly()); registerRoute(/^.*\/apps\/memories\/api\/.*/, new NetworkOnly());
registerRoute(/^.*\/.*$/, new NetworkFirst({ // Cache pages for same-origin requests only
cacheName: 'pages', registerRoute(
({ url }) => url.origin === self.location.origin,
new NetworkFirst({
cacheName: "pages",
plugins: [ plugins: [
new ExpirationPlugin({ new ExpirationPlugin({
maxAgeSeconds: 3600 * 24 * 7, // days maxAgeSeconds: 3600 * 24 * 7, // days
maxEntries: 2000, // assets maxEntries: 2000, // assets
}), }),
], ],
})); })
);
self.addEventListener('activate', event => { self.addEventListener("activate", (event) => {
// Take control of all pages under this SW's scope immediately, // Take control of all pages under this SW's scope immediately,
// instead of waiting for reload/navigation. // instead of waiting for reload/navigation.
event.waitUntil(self.clients.claim()); event.waitUntil(self.clients.claim());