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', '/common/back.svg',
// Utility JS // Utility JS
'/common/worker.js',
'/common/items.js',
'/common/back.js', '/common/back.js',
'/common/components.js',
'/common/items.js',
'/common/tabs.js', '/common/tabs.js',
'/common/trainId.js', '/common/trainId.js',
'/common/worker.js',
// Base // Base
'/base.css', '/base.css',
'/base.dark.css', '/base.dark.css',
// Pages // Pages
'/index.html',
'/index.js',
'/about.html', '/about.html',
'/about.js', '/about.js',
'/train.html', '/config-route.html',
'/train.js', '/config-route.js',
'/config-route.css',
'/view-train.html', '/index.html',
'/view-train.js', '/index.js',
'/view-train.css',
'/view-train.dark.css', '/route.html',
'/route.js',
'/route.css',
'/route.dark.css',
'/station.html', '/station.html',
'/station.js', '/station.js',
'/train.html',
'/train.js',
'/view-station.html', '/view-station.html',
'/view-station.js', '/view-station.js',
'/view-station.css', '/view-station.css',
'/view-station.dark.css', '/view-station.dark.css',
'/config-route.html', '/view-train.html',
'/config-route.js', '/view-train.js',
'/config-route.css', '/view-train.css',
'/view-train.dark.css',
'/route.html',
'/route.js',
'/route.css',
'/route.dark.css',
// API
API_TRAINS,
API_STATIONS,
// API_ITINERARIES,
]; ];
/** /**
@ -140,7 +136,9 @@ const putInCache = async (request, response) => {
const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => { const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => {
// First try to get the resource from the cache // 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 (responseFromCache) {
if (refreshAnyway || (responseFromCache.headers.has('SW-Cached-At') && Date.now() - new Date(responseFromCache.headers.get('SW-Cached-At')).valueOf() > 86400000)) { 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); 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 // 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) { if (responseFromCache) {
console.log('[nf] using cache response', responseFromCache) console.log('[nf] using cache response', responseFromCache)
return responseFromCache return responseFromCache
@ -255,7 +255,7 @@ self.addEventListener('fetch', (event) => {
cacheFirst({ cacheFirst({
request: event.request, request: event.request,
preloadResponsePromise: event.preloadResponse, preloadResponsePromise: event.preloadResponse,
refreshAnyway: [API_STATIONS, API_TRAINS].includes(event.request.url.split('?')[0]), refreshAnyway: [].includes(event.request.url.split('?')[0]),
}) })
) )
} }

Loading…
Cancel
Save