-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathworker.js
More file actions
108 lines (96 loc) · 3.33 KB
/
worker.js
File metadata and controls
108 lines (96 loc) · 3.33 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Server } from '@sveltejs/kit/internal/server';
// TODO: use prod variables when building
// TODO: fix kit virtual module type issues when this file is consumed by an app
import { version } from '__sveltekit/environment';
import {
manifest,
env,
remote_address,
base_path,
prerendered
} from '__sveltekit/vite-environment';
import * as Cache from 'worktop/cfw.cache';
const app_path = `/${manifest.appPath}`;
const immutable = `${app_path}/immutable/`;
const version_file = `${app_path}/version.json`;
const server = new Server(manifest);
await server.init({ env });
/**
* @param {Request} request
* @param {{ ASSETS: { fetch: typeof fetch } }} env
* @param {import('@cloudflare/workers-types').ExecutionContext} ctx
* @returns {Promise<Response>}
*/
export async function handleRequest(request, env, ctx) {
// skip cache if "cache-control: no-cache" in request
let pragma = request.headers.get('cache-control') || '';
let res = !pragma.includes('no-cache') && (await Cache.lookup(request));
if (res) return res;
let { pathname, search } = new URL(request.url);
try {
pathname = decodeURIComponent(pathname);
} catch {
// ignore invalid URI
}
const stripped_pathname = pathname.replace(/\/$/, '');
// files in /static, the service worker, and Vite imported server assets
let is_static_asset = false;
const filename = stripped_pathname.slice(base_path.length + 1);
if (filename) {
is_static_asset =
manifest.assets.has(filename) ||
manifest.assets.has(filename + '/index.html') ||
filename in manifest._.server_assets ||
filename + '/index.html' in manifest._.server_assets;
}
let location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/';
// TODO: we should only return the version var in dev because the version file is not written to disk
if (pathname === version_file) {
res = new Response(version);
// TODO: only in production
// }
// else if (
// is_static_asset ||
// prerendered.has(pathname) ||
// pathname === version_file ||
// pathname.startsWith(immutable)
// ) {
// res = await env.ASSETS.fetch(request);
} else if (location && prerendered.has(location)) {
// trailing slash redirect for prerendered pages
if (search) location += search;
res = new Response('', {
status: 308,
headers: {
location
}
});
} else {
// dynamically-generated pages
res = await server.respond(request, {
platform: {
env,
ctx,
context: ctx, // deprecated in favor of ctx
// @ts-expect-error webworker types from worktop are not compatible with Cloudflare Workers types
caches,
// @ts-expect-error the type is correct but ts is confused because platform.cf uses the type from index.ts while req.cf uses the type from index.d.ts
cf: request.cf
},
getClientAddress() {
if (remote_address) return remote_address;
throw new Error('Could not determine clientAddress');
// TODO: use the header in prod
// return request.headers.get('cf-connecting-ip');
}
});
}
// write to `Cache` only if response is not an error,
// let `Cache.save` handle the Cache-Control and Vary headers
pragma = res.headers.get('cache-control') || '';
return pragma && res.status < 400 ? Cache.save(request, res, ctx) : res;
}
// Without this, server file changes will invalidate the entire server module graph
if (import.meta.hot) {
import.meta.hot.accept();
}