You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
2.9 KiB
117 lines
2.9 KiB
2 years ago
|
const VERSION = 'v1'
|
||
|
const API_ORIGIN = 'https://scraper.infotren.dcdev.ro/'
|
||
|
const API_TRAINS = `${API_ORIGIN}v2/trains`
|
||
|
const API_STATIONS = `${API_ORIGIN}v2/stations`
|
||
|
|
||
|
self.addEventListener('install', (event) => {
|
||
|
event.waitUntil(
|
||
|
caches
|
||
|
.open(VERSION)
|
||
|
.then((cache) =>
|
||
|
cache.addAll([
|
||
|
// Root
|
||
|
'/',
|
||
|
|
||
|
// Utility JS
|
||
|
'/worker.js',
|
||
|
'/items.js',
|
||
|
'/back.js',
|
||
|
|
||
|
// Base
|
||
|
'/base.css',
|
||
|
|
||
|
// Pages
|
||
|
'/index.html',
|
||
|
|
||
|
'/about.html',
|
||
|
|
||
|
'/train.html',
|
||
|
'/train.js',
|
||
|
|
||
|
'/view-train.html',
|
||
|
'/view-train.js',
|
||
|
'/view-train.css',
|
||
|
|
||
|
// API
|
||
|
API_TRAINS,
|
||
|
API_STATIONS,
|
||
|
|
||
|
])
|
||
|
)
|
||
|
)
|
||
|
})
|
||
|
|
||
|
const deleteCache = (key) => caches.delete(key)
|
||
|
|
||
|
const deleteOldCaches = async () => {
|
||
|
const cacheKeepList = [VERSION]
|
||
|
const keyList = await caches.keys()
|
||
|
const cachesToDelete = keyList.filter((key) => !cacheKeepList.includes(key))
|
||
|
await Promise.all(cachesToDelete.map(deleteCache))
|
||
|
}
|
||
|
|
||
|
self.addEventListener('activate', (event) => {
|
||
|
event.waitUntil(deleteOldCaches())
|
||
|
})
|
||
|
|
||
|
// Enable navigation preload
|
||
|
const enableNavigationPreload = async () => {
|
||
|
if (self.registration.navigationPreload) {
|
||
|
// Enable navigation preloads!
|
||
|
await self.registration.navigationPreload.enable()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const putInCache = async (request, response) => {
|
||
|
const cache = await caches.open(VERSION)
|
||
|
await cache.put(request, response)
|
||
|
}
|
||
|
|
||
|
const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => {
|
||
|
// First try to get the resource from the cache
|
||
|
const responseFromCache = await caches.match(request)
|
||
|
if (responseFromCache) {
|
||
|
if (refreshAnyway) {
|
||
|
console.log('request in cache, refreshing anyway but returning cache', request);
|
||
|
((async () => {
|
||
|
try {
|
||
|
const response = await fetch(request)
|
||
|
if (response.ok) {
|
||
|
putInCache(request, response)
|
||
|
}
|
||
|
}
|
||
|
catch {}
|
||
|
})())
|
||
|
}
|
||
|
return responseFromCache
|
||
|
}
|
||
|
|
||
|
// Next try to use (and cache) the preloaded response, if it's there
|
||
|
const preloadResponse = await preloadResponsePromise
|
||
|
if (preloadResponse) {
|
||
|
console.info('using preload response', preloadResponse)
|
||
|
putInCache(request, preloadResponse.clone())
|
||
|
return preloadResponse
|
||
|
}
|
||
|
|
||
|
// Next try to get the resource from the network
|
||
|
const responseFromNetwork = await fetch(request)
|
||
|
// response may be used only once
|
||
|
// we need to save clone to put one copy in cache
|
||
|
// and serve second one
|
||
|
if (responseFromNetwork.ok) {
|
||
|
putInCache(request, responseFromNetwork.clone())
|
||
|
}
|
||
|
return responseFromNetwork;
|
||
|
}
|
||
|
|
||
|
self.addEventListener('fetch', (event) => {
|
||
|
event.respondWith(
|
||
|
cacheFirst({
|
||
|
request: event.request,
|
||
|
preloadResponsePromise: event.preloadResponse,
|
||
|
refreshAnyway: [API_STATIONS, API_TRAINS].includes(event.request.url),
|
||
|
})
|
||
|
)
|
||
|
})
|