Min Idzelis ed5b260eeb
feat: service worker cache static app resources, and all entry points (#18043)
* feat: service worker cache static app resources, and all entry points

* review comments

* review

* lint

* minor tweaks

* review comments

* optimize disabled cache

---------

Co-authored-by: Alex <alex.tran1502@gmail.com>
2025-06-12 19:33:29 -04:00

27 lines
924 B
TypeScript

/// <reference types="@sveltejs/kit" />
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="webworker" />
import { installBroadcastChannelListener } from './broadcast-channel';
import { addFilesToCache, deleteOldCaches } from './cache';
import { handleFetchEvent } from './fetch-event';
const sw = globalThis as unknown as ServiceWorkerGlobalScope;
const handleActivate = (event: ExtendableEvent) => {
event.waitUntil(sw.clients.claim());
// Remove previous cached data from disk
event.waitUntil(deleteOldCaches());
};
const handleInstall = (event: ExtendableEvent) => {
event.waitUntil(sw.skipWaiting());
// Create a new cache and add all files to it
event.waitUntil(addFilesToCache());
};
sw.addEventListener('install', handleInstall);
sw.addEventListener('activate', handleActivate);
sw.addEventListener('fetch', handleFetchEvent);
installBroadcastChannelListener();