UNPKG

microsite

Version:
43 lines (42 loc) 1.65 kB
import "preact/debug"; import { h, render } from "preact"; import { generateStaticPropsContext } from "../utils/router.js"; const noop = () => Promise.resolve(); async function csr(fileName, Page, initialProps = {}) { var _a, _b; let Component = null; let getStaticProps = noop; let getStaticPaths = noop; if (typeof Page === "function") Component = Page; const root = document.getElementById("__microsite"); let props = {}; let paths = []; if (Page.Component) { Component = Page.Component; getStaticProps = (_a = Page.getStaticProps) !== null && _a !== void 0 ? _a : noop; getStaticPaths = (_b = Page.getStaticPaths) !== null && _b !== void 0 ? _b : noop; } paths = await getStaticPaths({}).then((res) => res && res.paths); paths = paths && paths.map((pathOrParams) => generateStaticPropsContext(fileName, pathOrParams)); const match = paths && paths.find((ctx) => ctx.path === window.location.pathname || ctx.path === `${window.location.pathname}/index`); if (paths && !match) { /// @ts-expect-error const ErrorPage = await import("/web_modules/microsite/_error.js").then((mod) => mod.default); render(h(ErrorPage, { statusCode: 404 }, null), root); } else { let ctx = paths ? match : generateStaticPropsContext(fileName, window.location.pathname); props = await getStaticProps(ctx).then((res) => res && res.props); if (!props) props = initialProps; render(h(Component, props, null), root); } } export { csr };