ServiceWorker.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #if USE_DATA_CACHING
  2. const cacheName = {{{JSON.stringify(COMPANY_NAME + "-" + PRODUCT_NAME + "-" + PRODUCT_VERSION )}}};
  3. const contentToCache = [
  4. "Build/{{{ LOADER_FILENAME }}}",
  5. "Build/{{{ FRAMEWORK_FILENAME }}}",
  6. #if USE_THREADS
  7. "Build/{{{ WORKER_FILENAME }}}",
  8. #endif
  9. "Build/{{{ DATA_FILENAME }}}",
  10. "Build/{{{ CODE_FILENAME }}}",
  11. "TemplateData/style.css"
  12. ];
  13. #endif
  14. self.addEventListener('install', function (e) {
  15. console.log('[Service Worker] Install');
  16. #if USE_DATA_CACHING
  17. e.waitUntil((async function () {
  18. const cache = await caches.open(cacheName);
  19. console.log('[Service Worker] Caching all: app shell and content');
  20. await cache.addAll(contentToCache);
  21. })());
  22. #endif
  23. });
  24. #if USE_DATA_CACHING
  25. self.addEventListener('fetch', function (e) {
  26. e.respondWith((async function () {
  27. let response = await caches.match(e.request);
  28. console.log(`[Service Worker] Fetching resource: ${e.request.url}`);
  29. if (response) { return response; }
  30. response = await fetch(e.request);
  31. const cache = await caches.open(cacheName);
  32. console.log(`[Service Worker] Caching new resource: ${e.request.url}`);
  33. cache.put(e.request, response.clone());
  34. return response;
  35. })());
  36. });
  37. #endif