revalidate loaders on message reception

This commit is contained in:
m5r
2022-06-12 23:32:57 +02:00
parent ceaadc4f99
commit aac662f702
4 changed files with 22 additions and 10 deletions

View File

@ -117,7 +117,7 @@ export function fetchLoaderData(event: FetchEvent): Promise<Response> {
// and if we had returned a cached response
// tell the UI to fetch the latest data
console.debug("Revalidate loader data", path);
const channel = new BroadcastChannel("sw-messages");
const channel = new BroadcastChannel("revalidate");
channel.postMessage("revalidateLoaderData");
lastTimeRevalidated[path] = Date.now();
}
@ -193,7 +193,7 @@ export async function purgeMutatedLoaders(event: FetchEvent) {
const cachedPathname = new URL(loader.url).pathname;
const shouldPurge = cachedPathname.startsWith(rootPathname);
if (url.pathname === "/settings/phone") {
if (["/dev/null", "/settings/phone"].includes(url.pathname)) {
// changes phone number or twilio account credentials
// so purge messages and phone calls from cache
return (

View File

@ -15,12 +15,17 @@ export default async function handlePush(event: PushEvent) {
const payload: NotificationPayload = event.data!.json();
const options = Object.assign({}, defaultOptions, payload);
const revalidateChannel = new BroadcastChannel("revalidate");
// should revalidate just "/messages" and `/messages/${encodeURIComponent(payload.data.recipient)}`
revalidateChannel.postMessage("revalidateLoaderData");
revalidateChannel.close();
const clients = await self.clients.matchAll({ type: "window" });
const hasOpenTab = clients.some((client) => client.focused === true);
if (hasOpenTab) {
const channel = new BroadcastChannel("notifications");
channel.postMessage(JSON.stringify(payload));
channel.close();
const notifyChannel = new BroadcastChannel("notifications");
notifyChannel.postMessage(JSON.stringify(payload));
notifyChannel.close();
} else {
await self.registration.showNotification(payload.title, options);
await addBadge(1);