| |
| |
| |
|
|
| const CACHE_NAME = 'chassis-ocr-pwa-v1'; |
| const ASSETS_TO_CACHE = [ |
| '/', |
| '/index.html', |
| '/app.css', |
| '/app.js', |
| '/manifest.json', |
| '/icons/icon-192.png', |
| '/icons/icon-512.png' |
| ]; |
|
|
| |
| self.addEventListener('install', (event) => { |
| event.waitUntil( |
| caches.open(CACHE_NAME) |
| .then((cache) => { |
| console.log('[Service Worker] Caching App Shell...'); |
| return cache.addAll(ASSETS_TO_CACHE); |
| }) |
| .then(() => self.skipWaiting()) |
| ); |
| }); |
|
|
| |
| self.addEventListener('activate', (event) => { |
| event.waitUntil( |
| caches.keys().then((keyList) => { |
| return Promise.all(keyList.map((key) => { |
| if (key !== CACHE_NAME) { |
| console.log('[Service Worker] Removing old cache:', key); |
| return caches.delete(key); |
| } |
| })); |
| }).then(() => self.clients.claim()) |
| ); |
| }); |
|
|
| |
| self.addEventListener('fetch', (event) => { |
| const requestUrl = new URL(event.request.url); |
|
|
| |
| if (event.request.method !== 'GET' || requestUrl.pathname.startsWith('/api/')) { |
| return; |
| } |
|
|
| event.respondWith( |
| caches.match(event.request) |
| .then((cachedResponse) => { |
| if (cachedResponse) { |
| |
| fetch(event.request).then((networkResponse) => { |
| if (networkResponse && networkResponse.status === 200) { |
| caches.open(CACHE_NAME).then((cache) => { |
| cache.put(event.request, networkResponse); |
| }); |
| } |
| }).catch(() => {}); |
| |
| return cachedResponse; |
| } |
|
|
| |
| return fetch(event.request).then((networkResponse) => { |
| if (!networkResponse || networkResponse.status !== 200 || networkResponse.type !== 'basic') { |
| return networkResponse; |
| } |
| |
| |
| const responseToCache = networkResponse.clone(); |
| caches.open(CACHE_NAME).then((cache) => { |
| cache.put(event.request, responseToCache); |
| }); |
| |
| return networkResponse; |
| }); |
| }) |
| ); |
| }); |
|
|