-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathDocumentHydrationHelper.tsx
More file actions
70 lines (67 loc) · 2.04 KB
/
DocumentHydrationHelper.tsx
File metadata and controls
70 lines (67 loc) · 2.04 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
import { getRequestEvent, HydrationScript, NoHydration } from "solid-js/web";
import type { Asset, PageEvent } from "@solidjs/start/server";
import type { JSX } from "solid-js";
const assetMap = {
style: (props: { attrs: JSX.StyleHTMLAttributes<HTMLStyleElement>; children?: JSX.Element }) => (
<style {...props.attrs}>{props.children}</style>
),
link: (props: { attrs: JSX.LinkHTMLAttributes<HTMLLinkElement> }) => <link {...props.attrs} />,
script: (props: {
attrs: JSX.ScriptHTMLAttributes<HTMLScriptElement>;
key: string | undefined;
}) => {
return props.attrs.src ? (
<script {...props.attrs} id={props.key}>
{" "}
</script>
) : null;
},
};
export function renderAsset(asset: Asset) {
let { tag, attrs: { key, ...attrs } = { key: undefined }, children } = asset as any;
return (assetMap as any)[tag]({ attrs, key, children });
}
export default function () {
const context = getRequestEvent() as PageEvent;
// @ts-ignore
const nonce = context?.nonce;
return (
<>
<NoHydration>
<HydrationScript />
{context.assets.map((m: any) => renderAsset(m))}
{nonce ? (
<>
<script
nonce={nonce}
innerHTML={`window.manifest = ${JSON.stringify(context.manifest)}`}
/>
<script
type="module"
nonce={nonce}
async
src={
import.meta.env.MANIFEST["client"]!.inputs[
import.meta.env.MANIFEST["client"]!.handler
]!.output.path
}
/>
</>
) : (
<>
<script innerHTML={`window.manifest = ${JSON.stringify(context.manifest)}`} />
<script
type="module"
async
src={
import.meta.env.MANIFEST["client"]!.inputs[
import.meta.env.MANIFEST["client"]!.handler
]!.output.path
}
/>
</>
)}
</NoHydration>
</>
);
}