1 | import React, { createContext, useRef } from "preact/compat";
|
2 | export const __DocContext = createContext({
|
3 | head: { current: [] },
|
4 | hydrate: { current: [] },
|
5 | });
|
6 | export const __hydratedComponents = [];
|
7 | const unique = (value, index, self) => self.indexOf(value) === index;
|
8 | import render from "preact-render-to-string";
|
9 | export const Document = ({ hydrateExportManifest, page, styles = [], hasScripts = false, children, }) => {
|
10 | const head = useRef([]);
|
11 | const hydrate = useRef([]);
|
12 | const subtree = render(React.createElement(__DocContext.Provider, { value: { head, hydrate } }, children), {}, { pretty: true });
|
13 | const components = hydrate.current.map(({ name }) => name).filter(unique);
|
14 | if (hydrate.current.length > 0)
|
15 | __hydratedComponents.push({ page, components });
|
16 | const componentStyles = components
|
17 | .map((name) => {
|
18 | const found = hydrateExportManifest.find((entry) => entry.exports.includes(name));
|
19 | if (found)
|
20 | return found.styles;
|
21 | return null;
|
22 | })
|
23 | .filter((v) => v)
|
24 | .join("")
|
25 | .trim();
|
26 | return (React.createElement("html", null,
|
27 | React.createElement("head", null,
|
28 | React.createElement("meta", { charSet: "utf-8" }),
|
29 | React.createElement("meta", { name: "viewport", content: "width=device-width" }),
|
30 | React.createElement(React.Fragment, null, head.current),
|
31 | React.createElement("style", { dangerouslySetInnerHTML: {
|
32 | __html: "[data-hydrate]{display:contents;}",
|
33 | } }),
|
34 | styles.map((style) => (React.createElement("style", { dangerouslySetInnerHTML: { __html: style.trim() } }))),
|
35 | componentStyles && (React.createElement("style", { dangerouslySetInnerHTML: { __html: componentStyles } }))),
|
36 | React.createElement("body", null,
|
37 | React.createElement("div", { id: "__microsite", dangerouslySetInnerHTML: { __html: subtree } }),
|
38 | hydrate.current.length > 0 && (React.createElement("script", { type: "module", defer: true, src: `/_hydrate/pages/${page}.js` })),
|
39 | hasScripts ? (React.createElement(React.Fragment, null,
|
40 | React.createElement("script", { type: "module", src: "/index.js" }),
|
41 | React.createElement("script", Object.assign({}, { nomodule: "" }, { src: "https://unpkg.com/systemjs@2.0.0/dist/s.min.js" })),
|
42 | React.createElement("script", Object.assign({}, { nomodule: "" }, { src: "/index.legacy.js" })))) : null)));
|
43 | };
|