Continous Integration in der Praxis Gruppenarbeit
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
2.2 KiB

  1. // Caution! Be sure you understand the caveats before publishing an application with
  2. // offline support. See https://aka.ms/blazor-offline-considerations
  3. self.importScripts('./service-worker-assets.js');
  4. self.addEventListener('install', event => event.waitUntil(onInstall(event)));
  5. self.addEventListener('activate', event => event.waitUntil(onActivate(event)));
  6. self.addEventListener('fetch', event => event.respondWith(onFetch(event)));
  7. const cacheNamePrefix = 'offline-cache-';
  8. const cacheName = `${cacheNamePrefix}${self.assetsManifest.version}`;
  9. const offlineAssetsInclude = [ /\.dll$/, /\.pdb$/, /\.wasm/, /\.html/, /\.js$/, /\.json$/, /\.css$/, /\.woff$/, /\.png$/, /\.jpe?g$/, /\.gif$/, /\.ico$/, /\.blat$/, /\.dat$/ ];
  10. const offlineAssetsExclude = [ /^service-worker\.js$/ ];
  11. async function onInstall(event) {
  12. console.info('Service worker: Install');
  13. // Fetch and cache all matching items from the assets manifest
  14. const assetsRequests = self.assetsManifest.assets
  15. .filter(asset => offlineAssetsInclude.some(pattern => pattern.test(asset.url)))
  16. .filter(asset => !offlineAssetsExclude.some(pattern => pattern.test(asset.url)))
  17. .map(asset => new Request(asset.url, { integrity: asset.hash }));
  18. await caches.open(cacheName).then(cache => cache.addAll(assetsRequests));
  19. }
  20. async function onActivate(event) {
  21. console.info('Service worker: Activate');
  22. // Delete unused caches
  23. const cacheKeys = await caches.keys();
  24. await Promise.all(cacheKeys
  25. .filter(key => key.startsWith(cacheNamePrefix) && key !== cacheName)
  26. .map(key => caches.delete(key)));
  27. }
  28. async function onFetch(event) {
  29. let cachedResponse = null;
  30. if (event.request.method === 'GET') {
  31. // For all navigation requests, try to serve index.html from cache
  32. // If you need some URLs to be server-rendered, edit the following check to exclude those URLs
  33. const shouldServeIndexHtml = event.request.mode === 'navigate';
  34. const request = shouldServeIndexHtml ? 'index.html' : event.request;
  35. const cache = await caches.open(cacheName);
  36. cachedResponse = await cache.match(request);
  37. }
  38. return cachedResponse || fetch(event.request);
  39. }