-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssr.tsx
61 lines (53 loc) · 1.32 KB
/
ssr.tsx
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
// deno-lint-ignore-file no-explicit-any
/** @jsx h */
import {
getStyleTag,
h,
renderToString,
setup,
typography,
virtualSheet,
} from "./deps.ts";
let SHEET_SINGLETON: any = null;
function sheet(twOptions = {}) {
return SHEET_SINGLETON ?? (SHEET_SINGLETON = setupSheet(twOptions));
}
// Setup TW sheet singleton
function setupSheet(twOptions: Record<string, any>) {
const sheet = virtualSheet();
setup({ ...twOptions, sheet, plugins: { ...typography() } });
return sheet;
}
function makeHtml({ body, head, styleTag }: any) {
const html = (
<html lang="en-US">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
{head}
</head>
<body>
{body}
</body>
</html>
);
return renderToString(html).replace("<head>", `<head>${styleTag}`);
}
export function ssr(
func: CallableFunction,
options?: any,
) {
sheet(options?.tw ?? {}).reset();
const [head, body] = func();
const styleTag = getStyleTag(sheet());
return makeHtml({ body, head, styleTag });
}
export function html(response: string, status?: number) {
return new Response("<!DOCTYPE HTML>" + response, {
status,
headers: {
"content-type": "text/html; charset=UTF-8",
},
});
}
export { Fragment, h, tw } from "./deps.ts";