Browse Source

Improve caching

master
Kenneth Bruen 1 year ago
parent
commit
f3d474a9c7
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 54
      sw.js

54
sw.js

@ -12,52 +12,48 @@ const CACHE_FIRST = [
'/common/back.svg',
// Utility JS
'/common/worker.js',
'/common/items.js',
'/common/back.js',
'/common/components.js',
'/common/items.js',
'/common/tabs.js',
'/common/trainId.js',
'/common/worker.js',
// Base
'/base.css',
'/base.dark.css',
// Pages
'/index.html',
'/index.js',
'/about.html',
'/about.js',
'/train.html',
'/train.js',
'/config-route.html',
'/config-route.js',
'/config-route.css',
'/view-train.html',
'/view-train.js',
'/view-train.css',
'/view-train.dark.css',
'/index.html',
'/index.js',
'/route.html',
'/route.js',
'/route.css',
'/route.dark.css',
'/station.html',
'/station.js',
'/train.html',
'/train.js',
'/view-station.html',
'/view-station.js',
'/view-station.css',
'/view-station.dark.css',
'/config-route.html',
'/config-route.js',
'/config-route.css',
'/route.html',
'/route.js',
'/route.css',
'/route.dark.css',
// API
API_TRAINS,
API_STATIONS,
// API_ITINERARIES,
'/view-train.html',
'/view-train.js',
'/view-train.css',
'/view-train.dark.css',
];
/**
@ -140,7 +136,9 @@ const putInCache = async (request, response) => {
const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => {
// First try to get the resource from the cache
const responseFromCache = await caches.match(request)
const responseFromCache = await caches.match(request, {
ignoreSearch: true,
})
if (responseFromCache) {
if (refreshAnyway || (responseFromCache.headers.has('SW-Cached-At') && Date.now() - new Date(responseFromCache.headers.get('SW-Cached-At')).valueOf() > 86400000)) {
console.log('[cf] using cache response; refreshing anyway but returning cache', responseFromCache);
@ -232,7 +230,9 @@ const networkFirst = async ({ request, preloadResponsePromise }) => {
}
// Response from network wasn't ok, try to find in cache
const responseFromCache = await caches.match(request)
const responseFromCache = await caches.match(request, {
ignoreSearch: true,
})
if (responseFromCache) {
console.log('[nf] using cache response', responseFromCache)
return responseFromCache
@ -255,7 +255,7 @@ self.addEventListener('fetch', (event) => {
cacheFirst({
request: event.request,
preloadResponsePromise: event.preloadResponse,
refreshAnyway: [API_STATIONS, API_TRAINS].includes(event.request.url.split('?')[0]),
refreshAnyway: [].includes(event.request.url.split('?')[0]),
})
)
}

Loading…
Cancel
Save