-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.worker.js
68 lines (60 loc) · 1.99 KB
/
entry.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
import { manifest } from '../.svelte-kit/output/server/manifest.js';
import { Server } from '../.svelte-kit/output/server/index.js';
import revManifest from '../.svelte-kit/output/client/_app/immutable/manifest.json';
const server = new Server(manifest);
const revManifestEntries = Object.values(revManifest);
const CSS_FILES = revManifestEntries
.map((entry) => (entry.css ? `/_app/immutable/${entry.css}` : entry.css))
.flat()
.filter(Boolean);
const JS_FILES = revManifestEntries.map((entry) => `/_app/immutable/${entry.file}`);
const CORE_CACHE_FILES = JS_FILES.concat(CSS_FILES, ['/content.sqlite', '/sql-wasm.wasm']);
self.addEventListener('install', (event) => {
event.waitUntil(
caches
.open('core_cache')
.then((cache) => cache.addAll(CORE_CACHE_FILES))
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.open('core_cache').then((cache) => {
return cache
.keys()
.then((requests) => {
return Promise.all(
requests
.filter((request) => !CORE_CACHE_FILES.includes(getPathName(request.url)))
.map((cacheName) => cache.delete(cacheName))
);
})
.then(() => self.clients.claim());
})
);
});
self.addEventListener('fetch', async (event) => {
const { request } = event;
if (request.headers.get('Accept').includes('text/html')) {
event.respondWith(handler(request));
} else if (request.url.includes('/api')) {
event.respondWith(handler(request));
} else if (isCoreGetRequest(request)) {
event.respondWith(caches.open('core_cache').then((cache) => cache.match(request.url)));
}
});
async function handler(req) {
return await server.respond(req, {
platform: { env: {}, context: {} },
getClientAddress() {
return req.headers.get('cf-connecting-ip');
}
});
}
function isCoreGetRequest(request) {
return request.method === 'GET' && CORE_CACHE_FILES.includes(getPathName(request.url));
}
function getPathName(requestUrl) {
const url = new URL(requestUrl);
return url.pathname;
}