import grabContants from "../../../utils/grab-constants"; import serializeForScript from "../../../utils/serialize-for-script"; import type { LivePageDistGenParams } from "../../../types"; import isDevelopment from "../../../utils/is-development"; import grabWebPageHydrationScript from "./grab-web-page-hydration-script"; import grabWebMetaHTML from "./grab-web-meta-html"; import { log } from "../../../utils/log"; import { AppData } from "../../../data/app-data"; import _ from "lodash"; import grabDirNames from "../../../utils/grab-dir-names"; import { NoSEORobotsValue } from "../../../utils/disable-seo"; import grabClientPageProps from "../../../utils/grab-client-page-props"; const { ROOT_DIR } = grabDirNames(); export default async function genWebHTML({ component: Main, pageProps, bundledMap, module, routeParams, debug, root_module, }: LivePageDistGenParams) { const { ClientRootElementIDName, ClientWindowPagePropsName } = grabContants(); const { renderToReadableStream } = await import( `${ROOT_DIR}/node_modules/react-dom/server.js` ); const is_dev = isDevelopment(); if (debug) { log.info("component", Main); } if (!Main) { throw new Error(`Main Component not found!`); } // Only the client-safe part reaches the components and the HTML; // response_options (headers, cookies) etc. stay on the server. Head and // meta still get the full server result: they only render here. const client_props = grabClientPageProps(pageProps); let serializedProps = "{}"; try { serializedProps = serializeForScript(client_props); } catch (error) {} const page_hydration_script = await grabWebPageHydrationScript(); const root_meta = root_module?.meta ? typeof root_module.meta == "function" && routeParams ? await root_module.meta({ ctx: routeParams, serverRes: pageProps }) : typeof root_module.meta == "function" ? undefined : root_module.meta : undefined; const page_meta = module?.meta ? typeof module.meta == "function" && routeParams ? await module.meta({ ctx: routeParams, serverRes: pageProps }) : typeof module.meta == "function" ? undefined : module.meta : undefined; const html_props = { ...module?.html_props, ...root_module?.html_props, }; const Head = module?.Head; const RootHead = root_module?.Head; const dev = isDevelopment(); const final_meta = global.BUNEXT_CONFIG.disable_seo ? { ..._.merge(root_meta, page_meta), robots: NoSEORobotsValue } : _.merge(root_meta, page_meta); const public_envs = Object.fromEntries( Object.entries(process.env).filter(([k]) => k.startsWith("BUNEXT_PUBLIC_"), ), ); const client_process = { env: { NODE_ENV: dev ? "development" : "production", ...public_envs, ...global.BUNEXT_CONFIG.public_envs, }, }; let final_component = (
{final_meta ? grabWebMetaHTML({ meta: final_meta }) : null} {bundledMap?.css_path ? ( ) : null} {RootHead ? (