|
|
@ -1,14 +1,9 @@ |
|
|
|
const VERSION = 'v14' |
|
|
|
const VERSION = 'v15' |
|
|
|
const API_ORIGIN = 'https://scraper.infotren.dcdev.ro/' |
|
|
|
const API_ORIGIN = 'https://scraper.infotren.dcdev.ro/' |
|
|
|
const API_TRAINS = `${API_ORIGIN}v3/trains` |
|
|
|
const API_TRAINS = `${API_ORIGIN}v3/trains` |
|
|
|
const API_STATIONS = `${API_ORIGIN}v3/stations` |
|
|
|
const API_STATIONS = `${API_ORIGIN}v3/stations` |
|
|
|
|
|
|
|
|
|
|
|
self.addEventListener('install', (event) => { |
|
|
|
const CACHE_FIRST = [ |
|
|
|
event.waitUntil( |
|
|
|
|
|
|
|
caches |
|
|
|
|
|
|
|
.open(VERSION) |
|
|
|
|
|
|
|
.then((cache) => |
|
|
|
|
|
|
|
cache.addAll([ |
|
|
|
|
|
|
|
// Root
|
|
|
|
// Root
|
|
|
|
'/', |
|
|
|
'/', |
|
|
|
|
|
|
|
|
|
|
@ -43,8 +38,22 @@ self.addEventListener('install', (event) => { |
|
|
|
// API
|
|
|
|
// API
|
|
|
|
API_TRAINS, |
|
|
|
API_TRAINS, |
|
|
|
API_STATIONS, |
|
|
|
API_STATIONS, |
|
|
|
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param {string} url |
|
|
|
|
|
|
|
* @returns {boolean} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function shouldReturnFromCacheFirst(url) { |
|
|
|
|
|
|
|
return CACHE_FIRST.map(u => u.includes('://') ? u : self.location.origin + u).includes(url.split('?')[0]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
]) |
|
|
|
self.addEventListener('install', (event) => { |
|
|
|
|
|
|
|
event.waitUntil( |
|
|
|
|
|
|
|
caches |
|
|
|
|
|
|
|
.open(VERSION) |
|
|
|
|
|
|
|
.then((cache) => |
|
|
|
|
|
|
|
cache.addAll(CACHE_FIRST) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
}) |
|
|
|
}) |
|
|
@ -70,8 +79,25 @@ self.addEventListener('activate', (event) => { |
|
|
|
event.waitUntil(Promise.all([deleteOldCaches(), enableNavigationPreload()])) |
|
|
|
event.waitUntil(Promise.all([deleteOldCaches(), enableNavigationPreload()])) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @param {RequestInfo | URL} request |
|
|
|
|
|
|
|
* @param {Response} response |
|
|
|
|
|
|
|
* @returns {Promise<void>} |
|
|
|
|
|
|
|
*/ |
|
|
|
const putInCache = async (request, response) => { |
|
|
|
const putInCache = async (request, response) => { |
|
|
|
const cache = await caches.open(VERSION) |
|
|
|
const cache = await caches.open(VERSION) |
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// response.headers.set('SW-Cached-At', new Date().toISOString())
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// catch {
|
|
|
|
|
|
|
|
const headers = new Headers(response.headers) |
|
|
|
|
|
|
|
headers.set('SW-Cached-At', new Date().toISOString()) |
|
|
|
|
|
|
|
response = new Response(response.body, { |
|
|
|
|
|
|
|
status: response.status, |
|
|
|
|
|
|
|
statusText: response.statusText, |
|
|
|
|
|
|
|
headers, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
// }
|
|
|
|
await cache.put(request, response) |
|
|
|
await cache.put(request, response) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -80,17 +106,20 @@ const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => |
|
|
|
const responseFromCache = await caches.match(request) |
|
|
|
const responseFromCache = await caches.match(request) |
|
|
|
if (responseFromCache) { |
|
|
|
if (responseFromCache) { |
|
|
|
if (refreshAnyway) { |
|
|
|
if (refreshAnyway) { |
|
|
|
console.log('request in cache, refreshing anyway but returning cache', request); |
|
|
|
console.log('[cf] using cache response; refreshing anyway but returning cache', responseFromCache); |
|
|
|
((async () => { |
|
|
|
((async () => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const response = await fetch(request) |
|
|
|
const response = await fetch(request) |
|
|
|
if (response.ok) { |
|
|
|
if (response.ok) { |
|
|
|
putInCache(request, response) |
|
|
|
await putInCache(request, response) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
catch {} |
|
|
|
catch {} |
|
|
|
})()) |
|
|
|
})()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
console.log('[cf] using cache response', responseFromCache) |
|
|
|
|
|
|
|
} |
|
|
|
return responseFromCache |
|
|
|
return responseFromCache |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -98,12 +127,12 @@ const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => |
|
|
|
if (preloadResponsePromise) { |
|
|
|
if (preloadResponsePromise) { |
|
|
|
const preloadResponse = await preloadResponsePromise |
|
|
|
const preloadResponse = await preloadResponsePromise |
|
|
|
if (preloadResponse && preloadResponse.ok) { |
|
|
|
if (preloadResponse && preloadResponse.ok) { |
|
|
|
console.info('using preload response', preloadResponse) |
|
|
|
console.info('[cf] using preload response', preloadResponse) |
|
|
|
putInCache(request, preloadResponse.clone()) |
|
|
|
await putInCache(request, preloadResponse.clone()) |
|
|
|
return preloadResponse |
|
|
|
return preloadResponse |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
console.log('got not ok preloadResponse, ignoring', preloadResponse) |
|
|
|
console.log('[cf] got not ok preloadResponse, ignoring', preloadResponse) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -113,12 +142,66 @@ const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => |
|
|
|
// we need to save clone to put one copy in cache
|
|
|
|
// we need to save clone to put one copy in cache
|
|
|
|
// and serve second one
|
|
|
|
// and serve second one
|
|
|
|
if (responseFromNetwork.ok) { |
|
|
|
if (responseFromNetwork.ok) { |
|
|
|
putInCache(request, responseFromNetwork.clone()) |
|
|
|
console.log('[cf] using network response; ok so also cache', responseFromNetwork) |
|
|
|
|
|
|
|
await putInCache(request, responseFromNetwork.clone()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
console.log('[cf] using network not ok response', responseFromNetwork) |
|
|
|
} |
|
|
|
} |
|
|
|
return responseFromNetwork; |
|
|
|
return responseFromNetwork; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const networkFirst = async ({ request, preloadResponsePromise }) => { |
|
|
|
|
|
|
|
// First try to use (and cache) the preloaded response, if it's there
|
|
|
|
|
|
|
|
if (preloadResponsePromise) { |
|
|
|
|
|
|
|
const preloadResponse = await preloadResponsePromise |
|
|
|
|
|
|
|
if (preloadResponse && preloadResponse.ok) { |
|
|
|
|
|
|
|
console.info('[nf] using preload response', preloadResponse) |
|
|
|
|
|
|
|
await putInCache(request, preloadResponse.clone()) |
|
|
|
|
|
|
|
return preloadResponse |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
console.log('[nf] got not ok preloadResponse, ignoring', preloadResponse) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Next try to get the resource from the network
|
|
|
|
|
|
|
|
let responseFromNetwork |
|
|
|
|
|
|
|
let errorFromNetwork |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
responseFromNetwork = await fetch(request) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (e) { |
|
|
|
|
|
|
|
responseFromNetwork = null |
|
|
|
|
|
|
|
errorFromNetwork = e |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// If the response is ok, put it in cache and respond
|
|
|
|
|
|
|
|
if (responseFromNetwork && responseFromNetwork.ok) { |
|
|
|
|
|
|
|
console.log('[nf] using ok network response', responseFromNetwork) |
|
|
|
|
|
|
|
await putInCache(request, responseFromNetwork.clone()) |
|
|
|
|
|
|
|
return responseFromNetwork |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Response from network wasn't ok, try to find in cache
|
|
|
|
|
|
|
|
const responseFromCache = await caches.match(request) |
|
|
|
|
|
|
|
if (responseFromCache) { |
|
|
|
|
|
|
|
console.log('[nf] using cache response', responseFromCache) |
|
|
|
|
|
|
|
return responseFromCache |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If we didn't find a cached response, return the fresh network error
|
|
|
|
|
|
|
|
if (responseFromNetwork) { |
|
|
|
|
|
|
|
console.log('[nf] using network not ok response', responseFromNetwork) |
|
|
|
|
|
|
|
return responseFromNetwork |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
console.log('[nf] throwing network error', errorFromNetwork) |
|
|
|
|
|
|
|
throw errorFromNetwork |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self.addEventListener('fetch', (event) => { |
|
|
|
self.addEventListener('fetch', (event) => { |
|
|
|
|
|
|
|
if (shouldReturnFromCacheFirst(event.request.url)) { |
|
|
|
event.respondWith( |
|
|
|
event.respondWith( |
|
|
|
cacheFirst({ |
|
|
|
cacheFirst({ |
|
|
|
request: event.request, |
|
|
|
request: event.request, |
|
|
@ -126,4 +209,13 @@ self.addEventListener('fetch', (event) => { |
|
|
|
refreshAnyway: [API_STATIONS, API_TRAINS].includes(event.request.url.split('?')[0]), |
|
|
|
refreshAnyway: [API_STATIONS, API_TRAINS].includes(event.request.url.split('?')[0]), |
|
|
|
}) |
|
|
|
}) |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
event.respondWith( |
|
|
|
|
|
|
|
networkFirst({ |
|
|
|
|
|
|
|
request: event.request, |
|
|
|
|
|
|
|
preloadResponsePromise: event.preloadResponse, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|