/** * Service Worker for Shaarli Pro theme. * * Caches static theme assets (CSS, JS, icons, fonts) so the UI stays available * when the network is unreliable. Dynamic Shaarli pages are fetched from the * network first and only cached as a fallback. */ 'use strict'; const CACHE_NAME = 'shaarli-pro-v1.1.0'; const STATIC_ASSETS = [ 'dist/style.min.css', 'dist/themes.min.css', 'dist/custom_views.min.css', 'dist/awesomplete.min.css', 'dist/i18n.min.js', 'dist/a11y.min.js', 'dist/shaarit-rules.min.js', 'dist/theme.min.js', 'dist/navigation.min.js', 'dist/search.min.js', 'dist/bulk-actions.min.js', 'dist/script.min.js', 'dist/custom_views.min.js', 'dist/awesomplete.min.js', 'dist/metadata.min.js', 'img/favicon.png', 'img/icons/icon-192x192.png', 'img/icons/icon-512x512.png' ]; const isStaticAsset = function (url) { const pathname = url.pathname; return STATIC_ASSETS.some(function (asset) { return pathname.endsWith('/' + asset) || pathname === '/' + asset; }); }; self.addEventListener('install', function (event) { event.waitUntil( caches.open(CACHE_NAME).then(function (cache) { return cache.addAll(STATIC_ASSETS); }).catch(function () { // Silent failure: the SW still installs and will fetch on demand. }) ); self.skipWaiting(); }); self.addEventListener('activate', function (event) { event.waitUntil( caches.keys().then(function (cacheNames) { return Promise.all( cacheNames.map(function (cacheName) { if (cacheName !== CACHE_NAME) { return caches.delete(cacheName); } }) ); }) ); self.clients.claim(); }); self.addEventListener('fetch', function (event) { const request = event.request; const url = new URL(request.url); // Only handle GET requests. if (request.method !== 'GET') { return; } if (isStaticAsset(url)) { // Cache-first for theme assets. event.respondWith( caches.match(request).then(function (cached) { return cached || fetch(request).then(function (response) { if (!response || response.status !== 200 || response.type !== 'basic') { return response; } const clone = response.clone(); caches.open(CACHE_NAME).then(function (cache) { cache.put(request, clone); }); return response; }); }) ); } else { // Network-first for everything else (Shaarli pages, feeds, etc.). event.respondWith( fetch(request).catch(function () { return caches.match(request); }) ); } });