-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice-worker.js
86 lines (80 loc) · 2.95 KB
/
service-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* The cache name should change every time you want to "cache bust"
* i.e. if you want to change these files
* in a real-world app this would be handled by your build system
* e.g. Vite or Next.js would hash the files so the name is unique based on content
* (style-XYZ123.css etc)
*/
const CACHE_NAME = "mpwa-cache-v1";
const urlsToCache = [
"/",
"/assets/css/index.css",
"/assets/js/srcset-polyfill.js",
"/img/empty.svg",
"/dist/tvmjs.bundle.js",
"/dist/tvmjs_runtime.wasi.js",
"/dist/stable_diffusion.js",
"/dist/stable_diffusion_webgpu.wasm",
"/dist/scheduler_pndm_consts.json",
"/dist/scheduler_dpm_solver_multistep_consts.json",
"/dist/tokenizers-wasm/tokenizers_wasm_bg.wasm",
"/dist/tokenizers-wasm/tokenizers_wasm.js",
];
/**
* Listen for the install event, which fires when the service worker is installing.
* We use event.waitUntil() to ensure the install doesn't finished until our promise resolves
* so we don't do anything else until the initial caching is done.
*/
self.addEventListener("install", async (event) => {
console.log("installing!");
self.skipWaiting();
event.waitUntil(cache_assets());
});
async function cache_assets() {
const cache = await self.caches.open(CACHE_NAME);
return cache.addAll(urlsToCache);
}
/**
* Listen for the activate event, which is fired after installation
* Activate is when the service worker actually takes over from the previous
* version, which is a good time to clean up old caches.
* Again we use waitUntil() to ensure we don't move on until the old caches are deleted.
*/
self.addEventListener("activate", async (event) => {
console.log("activating!");
event.waitUntil(delete_old_caches());
});
async function delete_old_caches() {
// Get the keys of all the old caches
const keys = await caches.keys();
const deletePromises = keys
.filter((key) => key !== CACHE_NAME)
.map((key) => self.caches.delete(key));
return Promise.all(deletePromises);
}
/**
* Listen for browser fetch events.
* These fire any time the browser tries to load anything.
* This isn't just fetch() calls; clicking a <a href> triggers it too.
*/
self.addEventListener("fetch", (event) => {
console.log("fetching!");
event.respondWith(get_response(event.request));
});
/**
* We follow the "stale-while-revalidate" pattern:
* respond with the cached response immediately (if we have one)
* even though this might be "stale" (not the most recent version).
* In the background fetch the latest version and put that into cache
* on next request the user will get the latest version
*/
async function get_response(request) {
const cache = await self.caches.open(CACHE_NAME);
const cached_response = await cache.match(request);
// Important: we do not await here, since that would defeat the point of using the cache
const pending_response = fetch(request).then((response) => {
cache.put(request, response.clone());
return response;
});
return cached_response || pending_response;
}