UNPKG

4.58 kBJavaScriptView Raw
1var __rest = (this && this.__rest) || function (s, e) {
2 var t = {};
3 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4 t[p] = s[p];
5 if (s != null && typeof Object.getOwnPropertySymbols === "function")
6 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8 t[p[i]] = s[p[i]];
9 }
10 return t;
11};
12import { h, createContext, Fragment, } from "preact";
13import { useContext } from "preact/hooks";
14import { generateHydrateScript } from "./utils/hydration.js";
15export const __HeadContext = createContext({
16 head: { current: [] },
17});
18/** @internal */
19export const __InternalDocContext = createContext({});
20const _Document = () => {
21 return (h(Html, null,
22 h(Head, null),
23 h("body", null,
24 h(Main, null),
25 h(MicrositeScript, null))));
26};
27export const defineDocument = (Document, ctx) => {
28 return Object.assign(Document, ctx);
29};
30export const Document = defineDocument(_Document, {
31 async prepare({ renderPage }) {
32 const page = await renderPage();
33 return Object.assign({}, page);
34 },
35});
36export const Html = (_a) => {
37 var { lang = "en", dir = "ltr" } = _a, props = __rest(_a, ["lang", "dir"]);
38 return h("html", Object.assign({ lang: lang, dir: dir }, props));
39};
40export const Main = (props) => {
41 const { __renderPageResult } = useContext(__InternalDocContext);
42 return (h("div", Object.assign({}, props, { id: "__microsite", dangerouslySetInnerHTML: { __html: __renderPageResult } })));
43};
44export const Head = (_a) => {
45 var { children } = _a, props = __rest(_a, ["children"]);
46 const { dev = false, preconnect = [], basePath = "/", hasGlobalScript = false, preload = [], styles = [], __renderPageHead, } = useContext(__InternalDocContext);
47 const shouldIncludeBasePath = basePath !== "/";
48 const prefix = shouldIncludeBasePath ? "./" : "/";
49 return (h("head", Object.assign({}, props),
50 h("meta", Object.assign({}, { charset: "utf-8" })),
51 h("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0" }),
52 shouldIncludeBasePath && h("base", { href: basePath }),
53 preconnect.map((href) => (h("link", { rel: "preconnect", href: href }))),
54 hasGlobalScript && (h("link", { rel: "modulepreload", href: `${prefix}_static/chunks/_global.js` })),
55 preload.map((href) => (h("link", { rel: "modulepreload", href: href }))),
56 styles &&
57 styles.map((href) => (h("link", { rel: "preload", href: `${prefix}${href}`, as: "style" }))),
58 styles &&
59 styles.map((href) => (h("link", { rel: "stylesheet", href: `${prefix}${href}` }))),
60 children,
61 dev && h("meta", { name: "microsite:start" }),
62 h(Fragment, null, __renderPageHead),
63 dev && h("meta", { name: "microsite:end" })));
64};
65export const MicrositeScript = () => {
66 const { __csrUrl, debug, hasGlobalScript, basePath, scripts, dev, devProps, } = useContext(__InternalDocContext);
67 return (h(Fragment, null,
68 dev && (h(Fragment, null,
69 h("script", { "data-csr": "true", dangerouslySetInnerHTML: {
70 __html: `window.HMR_WEBSOCKET_URL = 'ws://localhost:3333';`,
71 } }),
72 h("script", { type: "module", src: "/_snowpack/hmr-client.js" }),
73 h("script", { type: "module", dangerouslySetInnerHTML: {
74 __html: `import csr from '${__csrUrl}';
75import Page from '${dev}';
76csr(Page, ${JSON.stringify(devProps)});`,
77 } }),
78 h("script", { type: "module", dangerouslySetInnerHTML: {
79 __html: `(async () => {
80\ttry { await import('/src/global/index.css.proxy.js'); } catch (e) {}
81\ttry {
82\t\tconst global = await import('/src/global/index.js').then(mod => mod.default);
83\t\tif (global) global();
84\t} catch (e) {}
85})()`,
86 } }))),
87 debug && (h("script", { dangerouslySetInnerHTML: {
88 __html: `window.__MICROSITE_DEBUG = true;`,
89 } })),
90 hasGlobalScript && (h("script", { type: "module", dangerouslySetInnerHTML: {
91 __html: `import global from '${basePath}_static/chunks/_global.js';\nglobal();`,
92 } })),
93 scripts && (h("script", { type: "module", async: true, dangerouslySetInnerHTML: {
94 __html: generateHydrateScript(scripts, { basePath }),
95 } }))));
96};