app-worker.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const cacheName = "app-" + "08848207254ed19206a7e0901b6c869dca9004c8";
  2. const resourcesToCache = ["/","/app.css","/app.js","/manifest.webmanifest","/wasm_exec.js","/web/app.wasm","/web/css/docs.css","/web/css/prism.css","/web/documents/home-next.md","/web/documents/home.md","/web/documents/updates.md","/web/documents/what-is-go-app.md","/web/icon.png","/web/js/prism.js","https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500\u0026display=swap","https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1013306768105236"];
  3. self.addEventListener("install", (event) => {
  4. console.log("installing app worker 08848207254ed19206a7e0901b6c869dca9004c8");
  5. event.waitUntil(
  6. caches
  7. .open(cacheName)
  8. .then((cache) => {
  9. return cache.addAll(resourcesToCache);
  10. })
  11. .then(() => {
  12. self.skipWaiting();
  13. })
  14. );
  15. });
  16. self.addEventListener("activate", (event) => {
  17. event.waitUntil(
  18. caches.keys().then((keyList) => {
  19. return Promise.all(
  20. keyList.map((key) => {
  21. if (key !== cacheName) {
  22. return caches.delete(key);
  23. }
  24. })
  25. );
  26. })
  27. );
  28. console.log("app worker 08848207254ed19206a7e0901b6c869dca9004c8 is activated");
  29. });
  30. self.addEventListener("fetch", (event) => {
  31. event.respondWith(
  32. caches.match(event.request).then((response) => {
  33. return response || fetch(event.request);
  34. })
  35. );
  36. });
  37. self.addEventListener("push", (event) => {
  38. if (!event.data || !event.data.text()) {
  39. return;
  40. }
  41. const notification = JSON.parse(event.data.text());
  42. if (!notification) {
  43. return;
  44. }
  45. const title = notification.title;
  46. delete notification.title;
  47. if (!notification.data) {
  48. notification.data = {};
  49. }
  50. let actions = [];
  51. for (let i in notification.actions) {
  52. const action = notification.actions[i];
  53. actions.push({
  54. action: action.action,
  55. path: action.path,
  56. });
  57. delete action.path;
  58. }
  59. notification.data.goapp = {
  60. path: notification.path,
  61. actions: actions,
  62. };
  63. delete notification.path;
  64. event.waitUntil(self.registration.showNotification(title, notification));
  65. });
  66. self.addEventListener("notificationclick", (event) => {
  67. event.notification.close();
  68. const notification = event.notification;
  69. let path = notification.data.goapp.path;
  70. for (let i in notification.data.goapp.actions) {
  71. const action = notification.data.goapp.actions[i];
  72. if (action.action === event.action) {
  73. path = action.path;
  74. break;
  75. }
  76. }
  77. event.waitUntil(
  78. clients
  79. .matchAll({
  80. type: "window",
  81. })
  82. .then((clientList) => {
  83. for (var i = 0; i < clientList.length; i++) {
  84. let client = clientList[i];
  85. if ("focus" in client) {
  86. client.focus();
  87. client.postMessage({
  88. goapp: {
  89. type: "notification",
  90. path: path,
  91. },
  92. });
  93. return;
  94. }
  95. }
  96. if (clients.openWindow) {
  97. return clients.openWindow(path);
  98. }
  99. })
  100. );
  101. });