service-worker.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // eslint-disable-next-line no-restricted-globals
  2. // eslint-disable-next-line no-unused-expressions
  3. /* eslint-disable no-restricted-globals */
  4. /* global importScripts, workbox */
  5. /**
  6. * Welcome to your Workbox-powered service worker!
  7. *
  8. * You'll need to register this file in your web app and you should
  9. * disable HTTP caching for this file too.
  10. * See https://goo.gl/nhQhGp
  11. *
  12. * The rest of the code is auto-generated. Please don't update this file
  13. * directly; instead, make changes to your Workbox build configuration
  14. * and re-run your build process.
  15. * See https://goo.gl/2aRDsh
  16. */
  17. importScripts("/workbox/workbox-sw.js");
  18. workbox.setConfig({
  19. modulePathPrefix: "/workbox/",
  20. });
  21. self.addEventListener("message", (event) => {
  22. if (event.data && event.data.type === "SKIP_WAITING") {
  23. self.skipWaiting();
  24. }
  25. });
  26. workbox.core.clientsClaim();
  27. workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);
  28. workbox.routing.registerNavigationRoute(
  29. workbox.precaching.getCacheKeyForURL("./index.html"),
  30. {
  31. blacklist: [/^\/_/, /\/[^/?]+\.[^/]+$/],
  32. },
  33. );
  34. // Cache relevant font files
  35. workbox.routing.registerRoute(
  36. new RegExp("/(fonts.css|.+.(ttf|woff2|otf))"),
  37. new workbox.strategies.StaleWhileRevalidate({
  38. cacheName: "fonts",
  39. plugins: [new workbox.expiration.Plugin({ maxEntries: 10 })],
  40. }),
  41. );
  42. self.addEventListener("fetch", (event) => {
  43. if (
  44. event.request.method === "POST" &&
  45. event.request.url.endsWith("/web-share-target")
  46. ) {
  47. return event.respondWith(
  48. (async () => {
  49. const formData = await event.request.formData();
  50. const file = formData.get("file");
  51. const webShareTargetCache = await caches.open("web-share-target");
  52. await webShareTargetCache.put("shared-file", new Response(file));
  53. return Response.redirect("/?web-share-target", 303);
  54. })(),
  55. );
  56. }
  57. });