aboutsummaryrefslogtreecommitdiff
path: root/public/sw.js
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2025-05-26 10:48:43 +0200
committerAriel Costas Guerrero <ariel@costas.dev>2025-05-26 10:48:43 +0200
commit5ced7f916d94e86e9a7ec164bee56f9a8e3a2a3a (patch)
treeb1ef5afa17b4a2f9fb2cbd683afc2fb6d905b5e1 /public/sw.js
parent4637373b50636e78dc2c7b6f99be879edb4ff7dc (diff)
Replace Azure SWA with custom server
Diffstat (limited to 'public/sw.js')
-rw-r--r--public/sw.js51
1 files changed, 0 insertions, 51 deletions
diff --git a/public/sw.js b/public/sw.js
deleted file mode 100644
index 70ca169..0000000
--- a/public/sw.js
+++ /dev/null
@@ -1,51 +0,0 @@
-const API_CACHE_NAME = 'api-cache-v1'
-const API_URL_PATTERN = /\/api\/(GetStopList)/;
-const API_MAX_AGE = 24 * 60 * 60 * 1000; // 24 hours
-
-self.addEventListener('install', (event) => {
- event.waitUntil(self.skipWaiting());
-});
-
-self.addEventListener('activate', (event) => {
- event.waitUntil(self.clients.claim());
-});
-
-self.addEventListener('fetch', async (event) => {
- const url = new URL(event.request.url);
-
- if (event.request.method !== "GET" || !API_URL_PATTERN.test(url.pathname)) {
- return;
- }
-
- event.respondWith(apiCacheFirst(event.request));
-});
-
-async function apiCacheFirst(request) {
- const cache = await caches.open(API_CACHE_NAME);
- const cachedResponse = await cache.match(request);
-
- if (cachedResponse) {
- const age = Date.now() - new Date(cachedResponse.headers.get('date')).getTime();
- if (age < API_MAX_AGE) {
- console.debug(`SW: Cache HIT for ${request.url}`);
- return cachedResponse;
- }
-
- // Cache is too old, fetch a fresh copy
- cache.delete(request);
- }
-
- try {
- const netResponse = await fetch(request);
-
- const responseToCache = netResponse.clone();
-
- cache.put(request, responseToCache);
-
- console.debug(`SW: Cache MISS for ${request.url}`);
-
- return netResponse;
- } catch (error) {
- throw error;
- }
-} \ No newline at end of file