-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathedge.js
53 lines (45 loc) · 1.09 KB
/
edge.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
import { Server } from '0SERVER';
import { manifest, prerendered } from 'MANIFEST';
const server = new Server(manifest);
const prefix = `/${manifest.appDir}/`;
/**
* @param { Request } request
* @param { any } context
* @returns { Promise<Response> }
*/
export default function handler(request, context) {
if (is_static_file(request)) {
// Static files can skip the handler
// TODO can we serve _app/immutable files with an immutable cache header?
return;
}
return server.respond(request, {
platform: { context },
getClientAddress() {
return context.ip;
}
});
}
/**
* @param {Request} request
*/
function is_static_file(request) {
const url = new URL(request.url);
// Assets in the app dir
if (url.pathname.startsWith(prefix)) {
return true;
}
// prerendered pages and index.html files
const pathname = url.pathname.replace(/\/$/, '');
let file = pathname.substring(1);
try {
file = decodeURIComponent(file);
} catch (err) {
// ignore
}
return (
manifest.assets.has(file) ||
manifest.assets.has(file + '/index.html') ||
prerendered.has(pathname || '/')
);
}