| 1 |
// @odoo-module ignore |
| 2 |
/* eslint-disable no-restricted-globals */ |
| 3 |
/* eslint-disable no-undef */ |
| 4 |
|
| 5 |
const cacheName = "odoo-pos-cache"; |
| 6 |
|
| 7 |
const fetchCacheRespond = async (event) => { |
| 8 |
const cache = await caches.open(cacheName); |
| 9 |
try { |
| 10 |
const response = await fetch(event.request); |
| 11 |
cache.put(event.request, response.clone()); |
| 12 |
return response; |
| 13 |
} catch { |
| 14 |
return await cache.match(event.request); |
| 15 |
} |
| 16 |
}; |
| 17 |
|
| 18 |
const cacheResources = async (event) => { |
| 19 |
const url = event.request.url; |
| 20 |
|
| 21 |
try { |
| 22 |
const cache = await caches.open(cacheName); |
| 23 |
await cache.add(url); |
| 24 |
} catch (error) { |
| 25 |
console.info("Failed to cache resource", url, error); |
| 26 |
} |
| 27 |
}; |
| 28 |
|
| 29 |
self.addEventListener("fetch", (event) => { |
| 30 |
const url = event.request.url; |
| 31 |
|
| 32 |
// Ignore Chrome extensions and dataset. Dataset will be cached in indexedDB. |
| 33 |
if ( |
| 34 |
url.includes("extension") || |
| 35 |
url.includes("web/dataset") || |
| 36 |
url.includes("hw_proxy/hello") || |
| 37 |
event.request.method !== "GET" |
| 38 |
) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
event.respondWith(fetchCacheRespond(event)); |
| 43 |
}); |
| 44 |
|
| 45 |
// Handle notification |
| 46 |
self.addEventListener("message", (event) => { |
| 47 |
const data = event.data; |
| 48 |
if (data.urlsToCache && navigator.onLine) { |
| 49 |
for (const url of data.urlsToCache) { |
| 50 |
cacheResources({ request: { url } }); |
| 51 |
} |
| 52 |
} |
| 53 |
}); |