Improve caching
This commit is contained in:
parent
90d1435fb8
commit
f3d474a9c7
1 changed files with 28 additions and 28 deletions
56
sw.js
56
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',
|
|
||||||
'/train.js',
|
|
||||||
|
|
||||||
'/view-train.html',
|
|
||||||
'/view-train.js',
|
|
||||||
'/view-train.css',
|
|
||||||
'/view-train.dark.css',
|
|
||||||
|
|
||||||
'/station.html',
|
|
||||||
'/station.js',
|
|
||||||
|
|
||||||
'/view-station.html',
|
|
||||||
'/view-station.js',
|
|
||||||
'/view-station.css',
|
|
||||||
'/view-station.dark.css',
|
|
||||||
|
|
||||||
'/config-route.html',
|
'/config-route.html',
|
||||||
'/config-route.js',
|
'/config-route.js',
|
||||||
'/config-route.css',
|
'/config-route.css',
|
||||||
|
|
||||||
|
'/index.html',
|
||||||
|
'/index.js',
|
||||||
|
|
||||||
'/route.html',
|
'/route.html',
|
||||||
'/route.js',
|
'/route.js',
|
||||||
'/route.css',
|
'/route.css',
|
||||||
'/route.dark.css',
|
'/route.dark.css',
|
||||||
|
|
||||||
// API
|
'/station.html',
|
||||||
API_TRAINS,
|
'/station.js',
|
||||||
API_STATIONS,
|
|
||||||
// API_ITINERARIES,
|
'/train.html',
|
||||||
|
'/train.js',
|
||||||
|
|
||||||
|
'/view-station.html',
|
||||||
|
'/view-station.js',
|
||||||
|
'/view-station.css',
|
||||||
|
'/view-station.dark.css',
|
||||||
|
|
||||||
|
'/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 }) => {
|
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…
Add table
Reference in a new issue