UNPKG

@mui/internal-docs-infra

Version:

MUI Infra - internal documentation creation tools.

97 lines (95 loc) 3.27 kB
import * as React from 'react'; import { jsx as _jsx } from "react/jsx-runtime"; /** * Server render path for a chunk: an async server component that produces the * content on the server, to be rendered under a Suspense boundary so React * streams the fallback until it resolves (per-chunk Suspense). It is a plain * async render function (no Node-only imports), so it bundles harmlessly with * the client surface - it just never runs there. * * With `initial`, it renders the server `InitialLoader` (the quick initial * state). Otherwise it prefers the server `Loader` component (dynamically * imported, never shipped to the client), falling back to a server-side * `data`-mode `load`. When none applies it renders the loading placeholder (the * client then takes over). * * Readiness/coordination is a client concern, so there is no gate here - the * parent Suspense owns the fallback, and the streamed content hydrates into the * client swap. */ export async function ChunkServerLoader(args) { const { config, props = {}, initial = false } = args; const options = props.loaderOptions ?? config.loaderOptions; const userProps = props.userProps ?? {}; // Server initial: render the InitialLoader's quick state (still `loading`, as // the full data is not in yet - a client source can upgrade it afterwards). if (initial && config.InitialLoader) { const loaded = await config.InitialLoader(); const Loaded = loaded.default; const loadingProps = { ...userProps, data: props.preloaded, loading: true }; return /*#__PURE__*/_jsx(Loaded, { ...loadingProps }); } // Server initial from a `data`-mode source: compute the quick `initial()` value // (synchronous, no await) on the server and render it into `ChunkContent` still // marked `loading`. `source.initial()` must return serializable data, since // `ChunkContent` may be a Client Component. `InitialLoader` above wins when both // are present. if (initial && config.source && config.source.mode === 'data' && config.source.initial) { const data = config.source.initial(options); const ChunkContent = config.ChunkContent; const loadingProps = { ...userProps, data, loading: true }; return /*#__PURE__*/_jsx(ChunkContent, { ...loadingProps }); } if (!initial && config.Loader) { const loaded = await config.Loader(); const Loaded = loaded.default; const contentProps = { ...userProps, data: props.preloaded, loading: false }; return /*#__PURE__*/_jsx(Loaded, { ...contentProps }); } if (!initial && config.source && config.source.mode === 'data') { const data = await config.source.load(options, new AbortController().signal); const ChunkContent = config.ChunkContent; const contentProps = { ...userProps, data, loading: false }; return /*#__PURE__*/_jsx(ChunkContent, { ...contentProps }); } const ChunkLoading = config.ChunkLoading; if (ChunkLoading) { const loadingProps = { ...userProps, data: props.preloaded, loading: true }; return /*#__PURE__*/_jsx(ChunkLoading, { ...loadingProps }); } return null; }