UNPKG

microsite

Version:
97 lines (96 loc) 4.58 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { h, createContext, Fragment, } from "preact"; import { useContext } from "preact/hooks"; import { generateHydrateScript } from "./utils/hydration.js"; export const __HeadContext = createContext({ head: { current: [] }, }); /** @internal */ export const __InternalDocContext = createContext({}); const _Document = () => { return (h(Html, null, h(Head, null), h("body", null, h(Main, null), h(MicrositeScript, null)))); }; export const defineDocument = (Document, ctx) => { return Object.assign(Document, ctx); }; export const Document = defineDocument(_Document, { async prepare({ renderPage }) { const page = await renderPage(); return Object.assign({}, page); }, }); export const Html = (_a) => { var { lang = "en", dir = "ltr" } = _a, props = __rest(_a, ["lang", "dir"]); return h("html", Object.assign({ lang: lang, dir: dir }, props)); }; export const Main = (props) => { const { __renderPageResult } = useContext(__InternalDocContext); return (h("div", Object.assign({}, props, { id: "__microsite", dangerouslySetInnerHTML: { __html: __renderPageResult } }))); }; export const Head = (_a) => { var { children } = _a, props = __rest(_a, ["children"]); const { dev = false, preconnect = [], basePath = "/", hasGlobalScript = false, preload = [], styles = [], __renderPageHead, } = useContext(__InternalDocContext); const shouldIncludeBasePath = basePath !== "/"; const prefix = shouldIncludeBasePath ? "./" : "/"; return (h("head", Object.assign({}, props), h("meta", Object.assign({}, { charset: "utf-8" })), h("meta", { name: "viewport", content: "width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0" }), shouldIncludeBasePath && h("base", { href: basePath }), preconnect.map((href) => (h("link", { rel: "preconnect", href: href }))), hasGlobalScript && (h("link", { rel: "modulepreload", href: `${prefix}_static/chunks/_global.js` })), preload.map((href) => (h("link", { rel: "modulepreload", href: href }))), styles && styles.map((href) => (h("link", { rel: "preload", href: `${prefix}${href}`, as: "style" }))), styles && styles.map((href) => (h("link", { rel: "stylesheet", href: `${prefix}${href}` }))), children, dev && h("meta", { name: "microsite:start" }), h(Fragment, null, __renderPageHead), dev && h("meta", { name: "microsite:end" }))); }; export const MicrositeScript = () => { const { __csrUrl, debug, hasGlobalScript, basePath, scripts, dev, devProps, } = useContext(__InternalDocContext); return (h(Fragment, null, dev && (h(Fragment, null, h("script", { "data-csr": "true", dangerouslySetInnerHTML: { __html: `window.HMR_WEBSOCKET_URL = 'ws://localhost:3333';`, } }), h("script", { type: "module", src: "/_snowpack/hmr-client.js" }), h("script", { type: "module", dangerouslySetInnerHTML: { __html: `import csr from '${__csrUrl}'; import Page from '${dev}'; csr(Page, ${JSON.stringify(devProps)});`, } }), h("script", { type: "module", dangerouslySetInnerHTML: { __html: `(async () => { \ttry { await import('/src/global/index.css.proxy.js'); } catch (e) {} \ttry { \t\tconst global = await import('/src/global/index.js').then(mod => mod.default); \t\tif (global) global(); \t} catch (e) {} })()`, } }))), debug && (h("script", { dangerouslySetInnerHTML: { __html: `window.__MICROSITE_DEBUG = true;`, } })), hasGlobalScript && (h("script", { type: "module", dangerouslySetInnerHTML: { __html: `import global from '${basePath}_static/chunks/_global.js';\nglobal();`, } })), scripts && (h("script", { type: "module", async: true, dangerouslySetInnerHTML: { __html: generateHydrateScript(scripts, { basePath }), } })))); };