From ccc3d8e127426d9f480317f6945f7f077dea3423 Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Sun, 28 Aug 2022 04:11:09 +0200 Subject: [PATCH] Move to API v3, support train groups --- sw.js | 27 ++++++++------ view-train.html | 9 ++++- view-train.js | 99 +++++++++++++++++++++++++++++++++++++++++++------ worker.js | 4 +- 4 files changed, 114 insertions(+), 25 deletions(-) diff --git a/sw.js b/sw.js index 2741ea8..5e919ac 100644 --- a/sw.js +++ b/sw.js @@ -1,6 +1,6 @@ -const VERSION = 'v1' +const VERSION = 'v5' const API_ORIGIN = 'https://scraper.infotren.dcdev.ro/' -const API_TRAINS = `${API_ORIGIN}v2/trains` +const API_TRAINS = `${API_ORIGIN}v3/trains` const API_STATIONS = `${API_ORIGIN}v2/stations` self.addEventListener('install', (event) => { @@ -50,10 +50,6 @@ const deleteOldCaches = async () => { await Promise.all(cachesToDelete.map(deleteCache)) } -self.addEventListener('activate', (event) => { - event.waitUntil(deleteOldCaches()) -}) - // Enable navigation preload const enableNavigationPreload = async () => { if (self.registration.navigationPreload) { @@ -62,6 +58,10 @@ const enableNavigationPreload = async () => { } } +self.addEventListener('activate', (event) => { + event.waitUntil(Promise.all([deleteOldCaches(), enableNavigationPreload()])) +}) + const putInCache = async (request, response) => { const cache = await caches.open(VERSION) await cache.put(request, response) @@ -87,11 +87,16 @@ const cacheFirst = async ({ request, preloadResponsePromise, refreshAnyway }) => } // 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 + if (preloadResponsePromise) { + const preloadResponse = await preloadResponsePromise + if (preloadResponse && preloadResponse.ok) { + console.info('using preload response', preloadResponse) + putInCache(request, preloadResponse.clone()) + return preloadResponse + } + else { + console.log('got not ok preloadResponse, ignoring', preloadResponse) + } } // Next try to get the resource from the network diff --git a/view-train.html b/view-train.html index dc0cc52..3da8e64 100644 --- a/view-train.html +++ b/view-train.html @@ -13,16 +13,21 @@ + -

Train Info

+

-
+ + +