crux.land

Api
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
export * from "https://deno.land/x/nano_jsx/mod.ts";
export { tw } from "https://cdn.skypack.dev/twind";
import {
Helmet,
renderSSR as nanoRender,
} from "https://deno.land/x/nano_jsx/mod.ts";
import { setup } from "https://cdn.skypack.dev/twind";
import {
getStyleTag,
virtualSheet,
} from "https://cdn.skypack.dev/twind/sheets";
import typography from "https://cdn.skypack.dev/@twind/typography";
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;
}
const html = ({ body, head, footer, styleTag }) => (`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
${head}
${styleTag}
</head>
<body>
${body}
${footer.join("\n")}
</body>
<html>
`);
export function ssr(render: CallableFunction, options?: any) {
sheet(options?.tw ?? {}).reset();
const app = nanoRender(render(), options);
const { body, head, footer } = Helmet.SSR(app);
const styleTag = getStyleTag(sheet());
return new Response(
html({ body, head, footer, styleTag }),
{ headers: { "content-type": "text/html" } },
);
}
export function memoizedSSR(render: CallableFunction, options?: any) {
let mresp = null;
return () => {
const resp = mresp ?? (mresp = ssr(render, options));
return resp.clone();
};
}
© 2020-2021 Denosaurs
GitHubDiscordTwitter