/*
 * Temporary cleanup service worker.
 *
 * We intentionally stop intercepting requests, clear legacy caches, and
 * unregister ourselves. This prevents stale HTML/chunk bundles from surviving
 * fresh web deployments and causing client-side hydration/runtime crashes.
 */

self.addEventListener("install", (event) => {
  event.waitUntil(self.skipWaiting());
});

self.addEventListener("activate", (event) => {
  event.waitUntil(
    (async () => {
      const cacheKeys = await caches.keys();
      await Promise.all(cacheKeys.map((cacheKey) => caches.delete(cacheKey)));

      const clients = await self.clients.matchAll({ type: "window", includeUncontrolled: true });
      await Promise.all(
        clients.map((client) =>
          "navigate" in client && typeof client.navigate === "function" ? client.navigate(client.url) : Promise.resolve()
        )
      );

      await self.registration.unregister();
    })()
  );
});

self.addEventListener("fetch", () => {
  // Intentionally empty: do not cache or intercept runtime requests.
});
