Skip to content

Commit

Permalink
Update service worker (#967)
Browse files Browse the repository at this point in the history
* Update app-worker.js

* registration to pop once page is loaded

* Update scripts.go
  • Loading branch information
maxence-charriere authored Jun 15, 2024
1 parent c72172c commit 2081190
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
60 changes: 33 additions & 27 deletions pkg/app/gen/app-worker.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
// -----------------------------------------------------------------------------
// PWA
// -----------------------------------------------------------------------------
const cacheName = "app-" + "{{.Version}}";
const resourcesToCache = {{.ResourcesToCache}};

self.addEventListener("install", (event) => {
console.log("installing app worker {{.Version}}");

event.waitUntil(
caches
.open(cacheName)
.then((cache) => {
return cache.addAll(resourcesToCache);
})
.then(() => {
self.skipWaiting();
})
);
event.waitUntil(installWorker());
});

async function installWorker() {
const cache = await caches.open(cacheName);
await cache.addAll(resourcesToCache);
await self.skipWaiting(); // Use this new service worker
}

self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key !== cacheName) {
return caches.delete(key);
}
})
);
})
);
event.waitUntil(deletePreviousCaches());
console.log("app worker {{.Version}} is activated");
});

async function deletePreviousCaches() {
keys = await caches.keys();
keys.forEach(async (key) => {
if (key != cacheName) {
console.log("deleting", key, "cache");
await caches.delete(key);
}
});
}

self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request);
})
);
event.respondWith(fetchWithCache(event.request));
});

async function fetchWithCache(request) {
cachedResponse = await caches.match(request);
if (cachedResponse) {
return cachedResponse;
}
return fetch(request);
}

// -----------------------------------------------------------------------------
// Push Notifications
// -----------------------------------------------------------------------------
self.addEventListener("push", (event) => {
if (!event.data || !event.data.text()) {
return;
Expand Down
23 changes: 12 additions & 11 deletions pkg/app/gen/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ goappInitWebAssembly();
// -----------------------------------------------------------------------------
async function goappInitServiceWorker() {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register(
"{{.WorkerJS}}"
);

goappServiceWorkerRegistration = registration;
goappSetupNotifyUpdate(registration);
goappSetupPushNotification();
} catch (err) {
console.error("goapp service worker registration failed", err);
}
window.addEventListener("load", async () => {
try {
const registration = await navigator.serviceWorker.register(
"{{.WorkerJS}}"
);
goappServiceWorkerRegistration = registration;
goappSetupNotifyUpdate(registration);
goappSetupPushNotification();
} catch (err) {
console.error("goapp service worker registration failed: ", err);
}
});
}
}

Expand Down
Loading

0 comments on commit 2081190

Please sign in to comment.