UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

58 lines (57 loc) 2 kB
import { SERVER_CONTEXT_KEY } from "../constants.mjs"; import { safeJsonStringify } from "../utils/htmlEscape.mjs"; import { SERVER_CONTEXT_POST_RENDER_STRING } from "../vite/constants.mjs"; import { useServerContext } from "../vite/one-server-only.mjs"; import { jsx } from "react/jsx-runtime"; function ServerContextScript() { if (process.env.VITE_ENVIRONMENT === "client") { return /* @__PURE__ */jsx("script", { async: true, href: SERVER_CONTEXT_KEY, suppressHydrationWarning: true, dangerouslySetInnerHTML: { __html: `` } }); } if (process.env.VITE_ENVIRONMENT === "ssr") { const context = useServerContext(); const cssUrls = context?.css || []; const { cssContents, ...restContext } = context || {}; const hasLeafLoaderData = restContext.loaderData !== void 0; const lastMatchIndex = (restContext.matches?.length ?? 0) - 1; const compactMatches = restContext.matches?.map((m, i) => ({ routeId: m.routeId, pathname: m.pathname, params: m.params, ...(hasLeafLoaderData && i === lastMatchIndex ? {} : { loaderData: m.loaderData }) })); const clientContext = { ...restContext, matches: compactMatches, cssInlineCount: cssContents?.length || 0, // use placeholder — postRenderData is set during render (after this component) // and replaced in the HTML string after rendering completes postRenderData: SERVER_CONTEXT_POST_RENDER_STRING }; return /* @__PURE__ */jsx("script", { async: true, href: SERVER_CONTEXT_KEY, suppressHydrationWarning: true, dangerouslySetInnerHTML: { __html: ` globalThis["${SERVER_CONTEXT_KEY}"] = ${safeJsonStringify(clientContext)}; globalThis.__oneLoadedCSS = new Set(${safeJsonStringify(cssUrls)}); ` } }); } return null; } export { ServerContextScript }; //# sourceMappingURL=ServerContextScript.mjs.map