-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tsx
More file actions
75 lines (68 loc) · 1.86 KB
/
main.tsx
File metadata and controls
75 lines (68 loc) · 1.86 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
/** @jsxImportSource preact */
import { renderToReadableStream } from "preact-render-to-string/stream";
import { serveFile } from "@std/http/file-server";
import { Document } from "#document";
const routes = [
{
pattern: new URLPattern({ pathname: "/" }),
file: "index.html",
},
{
pattern: new URLPattern({ pathname: "/standard-web-components" }),
file: "standard-web-components.html",
},
{
pattern: new URLPattern({ pathname: "/favicon.ico" }),
file: "images/favicon/favicon.ico",
},
{
pattern: new URLPattern({ pathname: "/favicon.svg" }),
file: "images/favicon/favicon.svg",
},
{
pattern: new URLPattern({ pathname: "/favicon-96x96.png" }),
file: "images/favicon/favicon-96x96.png",
},
{
pattern: new URLPattern({ pathname: "/site.webmanifest" }),
file: "images/favicon/site.webmanifest",
},
{
pattern: new URLPattern({ pathname: "/dom/main.js" }),
file: "build/main.js",
},
{
pattern: new URLPattern({ pathname: "/components/button.css" }),
file: "components/button.css",
},
{
pattern: new URLPattern({ pathname: "/components/counter.css" }),
file: "components/counter.css",
},
];
function document(): ReadableStream<Uint8Array> {
const stream = renderToReadableStream(<Document />);
const encoder = new TextEncoder();
return stream.pipeThrough(new TransformStream({
start(controller) {
controller.enqueue(encoder.encode("<!DOCTYPE html>"));
}
}));
}
export default {
fetch(request: Request) {
const { pathname } = new URL(request.url);
for (const route of routes) {
if (route.pattern.test({ pathname })) {
return serveFile(request, route.file);
}
}
const stream = document();
return new Response(stream, {
headers: {
"content-type": "text/html;charset=UTF-8",
},
});
},
};
export { document }