From e7d03260df44f2bf7f1e6b49c5707152d3d490b0 Mon Sep 17 00:00:00 2001 From: giac-mysten <124184614+giac-mysten@users.noreply.github.com> Date: Fri, 17 Jan 2025 14:17:04 +0100 Subject: [PATCH] fix(portal): service worker uninstall (#359) Signed-off-by: giac-mysten Co-authored-by: Alexandros Tzimas --- portal/server/public/walrus-sites-sw.js | 29 +++++++++++++++---------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/portal/server/public/walrus-sites-sw.js b/portal/server/public/walrus-sites-sw.js index c3c3919c..6ce30c81 100644 --- a/portal/server/public/walrus-sites-sw.js +++ b/portal/server/public/walrus-sites-sw.js @@ -1,15 +1,20 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -if ("serviceWorker" in navigator) { - navigator.serviceWorker - .getRegistrations() - .then(function (registrations) { - registrations.forEach(function (registration) { - registration.unregister(); - }); - }) - .catch(function (error) { - console.error("Error unregistering service workers:", error); - }); -} +// Inspired by https://github.com/NekR/self-destroying-sw +self.addEventListener("install", (_event) => { + self.skipWaiting(); +}); + +self.addEventListener("activate", (_event) => { + self.registration + .unregister() + .then(() => self.clients.matchAll()) + .then((clients) => { + clients.forEach((client) => { + if (client.url && "navigate" in client) { + client.navigate(client.url); + } + }); + }); +});