Skip to content

Commit

Permalink
Merge pull request #6 from icefoganalytics/caleb/offline-caching
Browse files Browse the repository at this point in the history
Caching fonts
  • Loading branch information
burkkyy authored Jul 4, 2024
2 parents e3a568d + 8290691 commit 68151c0
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions web/public/serviceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ console.log("Loading serviceWorker.js...");

import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";
import { registerRoute, NavigationRoute } from "workbox-routing";
import { NetworkFirst } from "workbox-strategies";
import { CacheFirst, NetworkFirst } from "workbox-strategies";
import { ExpirationPlugin } from "workbox-expiration";

self.skipWaiting();
Expand Down Expand Up @@ -39,20 +39,31 @@ registerRoute(new NavigationRoute(createHandlerBoundToURL("/index.html")));
* Attempt to cache simple api calls
*/

function handleReports() {
console.log("Attempting to cache locations!");
return new NetworkFirst({
cacheName: "api-location",
plugins: [
new ExpirationPlugin({
maxEntries: 16,
maxAgeSeconds: 120,
}),
],
});
}
// Cache fonts
registerRoute(({ request }) => {
return request.destination === 'font';
}, new CacheFirst({
cacheName: 'font-cache',
plugins: [
new ExpirationPlugin({
maxEntries: 64,
maxAgeSeconds: 60 * 60 * 24 * 30,
}),
],
})
);

// Cache reports
registerRoute(({ url }) => {
return url.pathname.startsWith("/api/location");
}, handleReports());
}, new NetworkFirst({
cacheName: "api-location",
plugins: [
new ExpirationPlugin({
maxEntries: 16,
maxAgeSeconds: 120,
}),
],
})
);

0 comments on commit 68151c0

Please sign in to comment.