|
1 | | -import { build, files, version } from '$service-worker'; |
| 1 | +import { version } from '$service-worker'; |
2 | 2 |
|
3 | | -const useCache = true; |
4 | 3 | const CACHE = `cache-${version}`; |
5 | 4 |
|
6 | | -export const APP_RESOURCES = [ |
7 | | - ...build, // the app itself |
8 | | - ...files, // everything in `static` |
9 | | -]; |
10 | | - |
11 | | -let cache: Cache | undefined; |
12 | | -export async function getCache() { |
13 | | - if (cache) { |
14 | | - return cache; |
| 5 | +let _cache: Cache | undefined; |
| 6 | +const getCache = async () => { |
| 7 | + if (_cache) { |
| 8 | + return _cache; |
15 | 9 | } |
16 | | - cache = await caches.open(CACHE); |
17 | | - return cache; |
18 | | -} |
19 | | - |
20 | | -export const isURL = (request: URL | RequestInfo): request is URL => (request as URL).href !== undefined; |
21 | | -export const isRequest = (request: RequestInfo): request is Request => (request as Request).url !== undefined; |
| 10 | + _cache = await caches.open(CACHE); |
| 11 | + return _cache; |
| 12 | +}; |
22 | 13 |
|
23 | | -export async function deleteOldCaches() { |
24 | | - for (const key of await caches.keys()) { |
25 | | - if (key !== CACHE) { |
26 | | - await caches.delete(key); |
27 | | - } |
28 | | - } |
29 | | -} |
30 | | - |
31 | | -const pendingRequests = new Map<string, AbortController>(); |
32 | | -const canceledRequests = new Set<string>(); |
33 | | - |
34 | | -export async function cancelLoad(urlString: string) { |
35 | | - const pending = pendingRequests.get(urlString); |
36 | | - if (pending) { |
37 | | - canceledRequests.add(urlString); |
38 | | - pending.abort(); |
39 | | - pendingRequests.delete(urlString); |
40 | | - } |
41 | | -} |
42 | | - |
43 | | -export async function getCachedOrFetch(request: URL | Request | string) { |
44 | | - const response = await checkCache(request); |
45 | | - if (response) { |
46 | | - return response; |
| 14 | +export const get = async (key: string) => { |
| 15 | + const cache = await getCache(); |
| 16 | + if (!cache) { |
| 17 | + return; |
47 | 18 | } |
48 | 19 |
|
49 | | - const urlString = getCacheKey(request); |
50 | | - const cancelToken = new AbortController(); |
| 20 | + return cache.match(key); |
| 21 | +}; |
51 | 22 |
|
52 | | - try { |
53 | | - pendingRequests.set(urlString, cancelToken); |
54 | | - const response = await fetch(request, { |
55 | | - signal: cancelToken.signal, |
56 | | - }); |
57 | | - |
58 | | - checkResponse(response); |
59 | | - await setCached(response, urlString); |
60 | | - return response; |
61 | | - } catch (error) { |
62 | | - if (canceledRequests.has(urlString)) { |
63 | | - canceledRequests.delete(urlString); |
64 | | - return new Response(undefined, { |
65 | | - status: 499, |
66 | | - statusText: 'Request canceled: Instructions unclear, accidentally interrupted myself', |
67 | | - }); |
68 | | - } |
69 | | - throw error; |
70 | | - } finally { |
71 | | - pendingRequests.delete(urlString); |
72 | | - } |
73 | | -} |
74 | | - |
75 | | -export async function checkCache(url: URL | Request | string) { |
76 | | - if (!useCache) { |
| 23 | +export const put = async (key: string, response: Response) => { |
| 24 | + if (response.status !== 200) { |
77 | 25 | return; |
78 | 26 | } |
79 | | - const cache = await getCache(); |
80 | | - return await cache.match(url); |
81 | | -} |
82 | 27 |
|
83 | | -export async function setCached(response: Response, cacheKey: URL | Request | string) { |
84 | | - if (cache && response.status === 200) { |
85 | | - const cache = await getCache(); |
86 | | - cache.put(cacheKey, response.clone()); |
| 28 | + const cache = await getCache(); |
| 29 | + if (!cache) { |
| 30 | + return; |
87 | 31 | } |
88 | | -} |
89 | 32 |
|
90 | | -function checkResponse(response: Response) { |
91 | | - if (!(response instanceof Response)) { |
92 | | - throw new TypeError('Fetch did not return a valid Response object'); |
93 | | - } |
94 | | -} |
| 33 | + cache.put(key, response.clone()); |
| 34 | +}; |
95 | 35 |
|
96 | | -export function getCacheKey(request: URL | Request | string) { |
97 | | - if (isURL(request)) { |
98 | | - return request.toString(); |
99 | | - } else if (isRequest(request)) { |
100 | | - return request.url; |
101 | | - } else { |
102 | | - return request; |
| 36 | +export const prune = async () => { |
| 37 | + for (const key of await caches.keys()) { |
| 38 | + if (key !== CACHE) { |
| 39 | + await caches.delete(key); |
| 40 | + } |
103 | 41 | } |
104 | | -} |
| 42 | +}; |
0 commit comments