The namegen5 website.
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.

86 lines
2.6 KiB

3 years ago
  1. import { timestamp, files, shell } from '@sapper/service-worker';
  2. const ASSETS = `cache${timestamp}`;
  3. // `shell` is an array of all the files generated by the bundler,
  4. // `files` is an array of everything in the `static` directory
  5. const to_cache = shell.concat(files);
  6. const staticAssets = new Set(to_cache);
  7. self.addEventListener('install', event => {
  8. event.waitUntil(
  9. caches
  10. .open(ASSETS)
  11. .then(cache => cache.addAll(to_cache))
  12. .then(() => {
  13. self.skipWaiting();
  14. })
  15. );
  16. });
  17. self.addEventListener('activate', event => {
  18. event.waitUntil(
  19. caches.keys().then(async keys => {
  20. // delete old caches
  21. for (const key of keys) {
  22. if (key !== ASSETS) await caches.delete(key);
  23. }
  24. self.clients.claim();
  25. })
  26. );
  27. });
  28. /**
  29. * Fetch the asset from the network and store it in the cache.
  30. * Fall back to the cache if the user is offline.
  31. */
  32. async function fetchAndCache(request) {
  33. const cache = await caches.open(`offline${timestamp}`)
  34. try {
  35. const response = await fetch(request);
  36. cache.put(request, response.clone());
  37. return response;
  38. } catch (err) {
  39. const response = await cache.match(request);
  40. if (response) return response;
  41. throw err;
  42. }
  43. }
  44. self.addEventListener('fetch', event => {
  45. if (event.request.method !== 'GET' || event.request.headers.has('range')) return;
  46. const url = new URL(event.request.url);
  47. // don't try to handle e.g. data: URIs
  48. const isHttp = url.protocol.startsWith('http');
  49. const isDevServerRequest = url.hostname === self.location.hostname && url.port !== self.location.port;
  50. const isStaticAsset = url.host === self.location.host && staticAssets.has(url.pathname);
  51. const skipBecauseUncached = event.request.cache === 'only-if-cached' && !isStaticAsset;
  52. if (isHttp && !isDevServerRequest && !skipBecauseUncached) {
  53. event.respondWith(
  54. (async () => {
  55. // always serve static files and bundler-generated assets from cache.
  56. // if your application has other URLs with data that will never change,
  57. // set this variable to true for them and they will only be fetched once.
  58. const cachedAsset = isStaticAsset && await caches.match(event.request);
  59. // for pages, you might want to serve a shell `service-worker-index.html` file,
  60. // which Sapper has generated for you. It's not right for every
  61. // app, but if it's right for yours then uncomment this section
  62. /*
  63. if (!cachedAsset && url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) {
  64. return caches.match('/service-worker-index.html');
  65. }
  66. */
  67. return cachedAsset || fetchAndCache(event.request);
  68. })()
  69. );
  70. }
  71. });