1 // Used for filtering matches based on status code, header, or both
2 import { CacheableResponsePlugin } from 'workbox-cacheable-response';
3 // Used to limit entries in cache, remove entries after a certain period of time
4 import { ExpirationPlugin } from 'workbox-expiration';
5 import { precacheAndRoute } from 'workbox-precaching/precacheAndRoute';
6 import { registerRoute } from 'workbox-routing';
7 import { CacheFirst, NetworkFirst } from 'workbox-strategies';
9 // To remove debug logs in dev mode
10 // self.__WB_DISABLE_DEV_LOGS = true;
12 const manifest = self.__WB_MANIFEST.filter(({ url }) => {
13 return !url.includes('htaccess');
16 precacheAndRoute(manifest);
18 const pagesStrategy = new NetworkFirst({
19 // Put all cached files in a cache named 'pages'
22 // Ensure that only requests that result in a 200 status are cached
23 new CacheableResponsePlugin({
29 // Cache page navigations (html) with a Network First strategy
31 ({ url, request }) => {
33 request.mode === 'navigate' && // The request is a navigation to a new page
34 !url.searchParams.has('no-cache') && // Ignore urls with ?no-cache query parameter
35 !['/create'].some((path) => url.pathname.startsWith(path)) // Ignore urls from .htaccess
38 // Use a Network First caching strategy
40 const path = url.pathname.startsWith('/eo') ? '/eo.html' : '/index.html';
41 const request = new Request(path);
42 return pagesStrategy.handle({ request, event });
46 // Cache images, CSS, JS, and Web Worker with a Cache First strategy
48 // Check to see if the request's destination is style for an image
50 request.destination === 'style' ||
51 request.destination === 'script' ||
52 request.destination === 'worker' ||
53 request.destination === 'image',
54 // Use a Cache First caching strategy
56 // Put all cached files in a cache named 'assets'
59 // Ensure that only requests that result in a 200 status are cached
60 new CacheableResponsePlugin({
63 // Don't cache more than 200 items, and expire them after 30 days
64 new ExpirationPlugin({
66 maxAgeSeconds: 60 * 60 * 24 * 30, // 30 Days