-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
51 lines (41 loc) 路 1.24 KB
/
Copy pathmain.js
File metadata and controls
51 lines (41 loc) 路 1.24 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
import { Hono } from "https://deno.land/x/hono/mod.ts";
import { serve } from "https://deno.land/std/http/server.ts";
import { serveStatic } from "https://deno.land/x/hono/middleware.ts";
import { h } from "preact";
import renderToString from "preact-render-to-string";
import { Router } from "wouter-preact";
import { App } from "./App.jsx";
const { imports: importMap } = JSON.parse(await Deno.readTextFile("./deno.jsonc"));
const app = new Hono();
app.get("/static/*", serveStatic({ root: "./" }));
app.get("*", (c) => {
const { search } = new URL(c.req.url);
const rendered = renderToString(
h(
Router,
{
ssrPath: c.req.path, // Provide current location to wouter
ssrSearch: search,
},
h(App)
)
);
return c.html(`
<html>
<head>
<title>Deno + Preact SSR</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@exampledev/new.css@1.1.2/new.min.css">
</head>
<body>
<div id="app">${rendered}</div>
<script type="importmap">
{
"imports": ${JSON.stringify(importMap)}
}
</script>
<script type="module" src="/static/client.js"></script>
</body>
</html>
`);
});
serve(app.fetch);