Skip to content

Instantly share code, notes, and snippets.

@Aybee5
Created July 17, 2020 14:08
Show Gist options
  • Select an option

  • Save Aybee5/838c4aee24e0a1ff29df0673e383b6be to your computer and use it in GitHub Desktop.

Select an option

Save Aybee5/838c4aee24e0a1ff29df0673e383b6be to your computer and use it in GitHub Desktop.

Revisions

  1. Aybee5 created this gist Jul 17, 2020.
    70 changes: 70 additions & 0 deletions remindo-sw-beautify.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    let dynamicCacheName = "dynamic",
    showNotifications = (t) => {
    self.registration.showNotification(t, {
    body: "Reminder",
    badge: "https://ibb.co/Lp2LyG3",
    tag: Date.now().toString(36),
    actions: [
    { action: "view", title: "View" },
    { action: "discard", title: "Discard" },
    ],
    renotify: !0,
    silent: !1,
    vibrate: [250, 100, 200],
    requireInteraction: !0,
    });
    };
    const openApp = () => {
    event.waitUntil(
    (async function() {
    const t = await clients.matchAll({ includeUncontrolled: !0 });
    let e,
    i = "https://remindo.netlify.app";
    for (const n of t)
    if (n.url.indexOf(i) >= 0) {
    n.focus(), (e = n);
    break;
    }
    e || (e = await clients.openWindow(i));
    })()
    );
    };
    self.addEventListener("install", (t) => {
    console.log("sw installed successfully", t);
    }),
    self.addEventListener("activate", (t) => {
    console.log("sw activated", t), self.skipWaiting();
    }),
    self.addEventListener("fetch", (t) => {
    0 === t.request.url.indexOf("http") &&
    t.respondWith(
    caches
    .open(dynamicCacheName)
    .then((e) =>
    e
    .match(t.request)
    .then(
    (i) =>
    i ||
    fetch(t.request).then((i) => (e.put(t.request, i.clone()), i))
    )
    )
    .catch((t) => {
    console.log("sw error", t);
    })
    );
    }),
    self.addEventListener("notificationclick", (t) => {
    switch ((t.notification.close(), t.action)) {
    case "view":
    default:
    openApp();
    }
    }),
    self.addEventListener("message", (t) => {
    if (t.data.time && t.data.title) {
    let e = t.data.time,
    i = t.data.title;
    setTimeout(showNotifications, e, i);
    }
    });