service-worker.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // in dev, `process` is undefined because this file is not compiled until build
  18. const IS_DEVELOPMENT =
  19. typeof process === "undefined" || process.env.NODE_ENV !== "production";
  20. if (IS_DEVELOPMENT) {
  21. importScripts(
  22. "https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js",
  23. );
  24. workbox.setConfig({
  25. debug: true,
  26. });
  27. } else {
  28. importScripts("/workbox/workbox-sw.js");
  29. workbox.setConfig({
  30. modulePathPrefix: "/workbox/",
  31. });
  32. }
  33. self.addEventListener("message", (event) => {
  34. if (event.data && event.data.type === "SKIP_WAITING") {
  35. self.skipWaiting();
  36. }
  37. });
  38. workbox.core.clientsClaim();
  39. if (!IS_DEVELOPMENT) {
  40. workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);
  41. workbox.routing.registerNavigationRoute(
  42. workbox.precaching.getCacheKeyForURL("./index.html"),
  43. {
  44. blacklist: [/^\/_/, /\/[^/?]+\.[^/]+$/],
  45. },
  46. );
  47. }
  48. // Cache relevant font files
  49. workbox.routing.registerRoute(
  50. new RegExp("/(fonts.css|.+.(ttf|woff2|otf))"),
  51. new workbox.strategies.StaleWhileRevalidate({
  52. cacheName: "fonts",
  53. plugins: [new workbox.expiration.Plugin({ maxEntries: 10 })],
  54. }),
  55. );
  56. self.addEventListener("fetch", (event) => {
  57. if (
  58. event.request.method === "POST" &&
  59. event.request.url.endsWith("/web-share-target")
  60. ) {
  61. return event.respondWith(
  62. (async () => {
  63. const formData = await event.request.formData();
  64. const file = formData.get("file");
  65. const webShareTargetCache = await caches.open("web-share-target");
  66. await webShareTargetCache.put("shared-file", new Response(file));
  67. return Response.redirect("/?web-share-target", 303);
  68. })(),
  69. );
  70. }
  71. });