-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathworker.ts
More file actions
34 lines (27 loc) · 713 Bytes
/
Copy pathworker.ts
File metadata and controls
34 lines (27 loc) · 713 Bytes
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
interface Env {
ASSETS: Fetcher;
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
let path = url.pathname;
if (path === '/') {
path = '/index.html';
}
if (!path.includes('.') && !path.endsWith('/')) {
path = path + '.html';
}
if (path.endsWith('/')) {
path = path + 'index.html';
}
try {
const asset = await env.ASSETS.fetch(new URL(path, request.url));
if (asset.status === 404) {
return await env.ASSETS.fetch(new URL('/index.html', request.url));
}
return asset;
} catch {
return new Response('Not Found', { status: 404 });
}
},
};