Email for professionals and teams
update pwa
Hieu Nguyen (Jack)3d70b293b45a
diff --git a/public/_headers b/public/_headersindex 3b460e6..22fba0a 100644--- a/public/_headers+++ b/public/_headers@@ -1,2 +1,5 @@/_next/static/*Cache-Control: public,max-age=31536000,immutable++/sw.js+ Cache-Control: no-cachediff --git a/public/sw.js b/public/sw.jsindex 4134e82..387be42 100644--- a/public/sw.js+++ b/public/sw.js@@ -1,3 +1,17 @@+self.addEventListener("install", () => {+ console.log("[sw] installed, activating immediately");+ self.skipWaiting();+});++self.addEventListener("activate", (event) => {+ console.log("[sw] activated, claiming clients");+ event.waitUntil(self.clients.claim());+});++self.addEventListener("message", (event) => {+ if (event.data?.type === "SKIP_WAITING") self.skipWaiting();+});+const DEFAULT_NOTIFICATION = {title: "New email",body: "You have a new message",diff --git a/src/app/manifest.ts b/src/app/manifest.tsindex 4387497..113ea18 100644--- a/src/app/manifest.ts+++ b/src/app/manifest.ts@@ -5,6 +5,8 @@ export default function manifest(): MetadataRoute.Manifest {name: "Mailflare",short_name: "Mailflare",description: "Email, anywhere.",+ id: "/",+ scope: "/",start_url: "/inbox",display: "standalone",background_color: "#ffffff",diff --git a/src/components/pwa-service-worker.tsx b/src/components/pwa-service-worker.tsxindex d01e428..4823855 100644--- a/src/components/pwa-service-worker.tsx+++ b/src/components/pwa-service-worker.tsx@@ -3,12 +3,67 @@import { useEffect } from "react";import { registerMailflareServiceWorker } from "@/lib/push/client";+const UPDATE_CHECK_INTERVAL_MS = 60 * 60 * 1000;+export function PwaServiceWorker() {useEffect(() => {if (!("serviceWorker" in navigator)) return;- void registerMailflareServiceWorker().catch((error) => {- console.warn("Mailflare service worker registration failed", error);- });+ let cancelled = false;+ const cleanups: Array<() => void> = [];+ let timer: ReturnType<typeof setInterval> | undefined;+ const hadController = Boolean(navigator.serviceWorker.controller);++ const checkForUpdate = (registration: ServiceWorkerRegistration) => {+ registration.update().catch((error) => {+ console.warn("[pwa] update check failed", error);+ });+ };++ const watchInstalling = (registration: ServiceWorkerRegistration) => {+ const worker = registration.installing;+ if (!worker) return;+ console.log("[pwa] new version available, downloading");+ worker.addEventListener("statechange", () => {+ if (worker.state === "installed") {+ console.log("[pwa] new version ready");+ // The worker normally skips waiting itself; this covers one that was already waiting.+ registration.waiting?.postMessage({ type: "SKIP_WAITING" });+ }+ });+ };++ const onControllerChange = () => {+ // The first claim of a fresh install is not an update.+ if (!hadController) return;+ console.log("[pwa] new version active; it applies fully on next reload");+ };+ navigator.serviceWorker.addEventListener("controllerchange", onControllerChange);++ void registerMailflareServiceWorker()+ .then((registration) => {+ if (cancelled) return;+ console.log("[pwa] service worker registered");+ registration.addEventListener("updatefound", () => watchInstalling(registration));+ if (registration.waiting && navigator.serviceWorker.controller) {+ registration.waiting.postMessage({ type: "SKIP_WAITING" });+ }+ timer = setInterval(() => checkForUpdate(registration), UPDATE_CHECK_INTERVAL_MS);+ const onVisible = () => {+ if (document.visibilityState === "visible") checkForUpdate(registration);+ };+ document.addEventListener("visibilitychange", onVisible);+ cleanups.push(() => document.removeEventListener("visibilitychange", onVisible));+ })+ .catch((error) => {+ console.warn("Mailflare service worker registration failed", error);+ });++ return () => {+ cancelled = true;+ if (timer) clearInterval(timer);+ navigator.serviceWorker.removeEventListener("controllerchange", onControllerChange);+ cleanups.forEach((fn) => fn());+ };}, []);return null;diff --git a/src/lib/push/client.ts b/src/lib/push/client.tsindex 69fea32..43d6239 100644--- a/src/lib/push/client.ts+++ b/src/lib/push/client.ts@@ -32,7 +32,7 @@ export function browserSupportsPush(): boolean {export async function registerMailflareServiceWorker(): Promise<ServiceWorkerRegistration> {if (!("serviceWorker" in navigator)) throw new Error("Service workers are not supported by this browser");- const registration = await navigator.serviceWorker.register("/sw.js", { scope: "/" });+ const registration = await navigator.serviceWorker.register("/sw.js", { scope: "/", updateViaCache: "none" });return registration;}