@mui/internal-docs-infra
Version:
MUI Infra - internal documentation creation tools.
38 lines (36 loc) • 1.38 kB
JavaScript
'use client';
import * as React from 'react';
import { ChunkContext } from "./ChunkContext.mjs";
import { jsx as _jsx } from "react/jsx-runtime";
/**
* Layout-level provider that supplies a client `StreamSource` to descendant
* chunks, dynamically importing the loader module only when a chunk first needs
* to load - so the loaders stay out of the initial bundle and are never
* imported when chunks are precomputed/preloaded. The import promise is cached,
* so many chunks share one fetch (mirrors how `CodeProvider` lazily provides
* its parser/loaders).
*/
export function ChunkProvider({
children,
source
}) {
// Hold the in-flight/resolved import so the first caller triggers the import
// and the rest reuse its promise. Written only inside the callback (never
// during render).
const cacheRef = React.useRef(null);
const resolveSource = React.useCallback(() => {
if (!cacheRef.current) {
cacheRef.current = source().then(loaded => loaded.default);
}
return cacheRef.current;
}, [source]);
const value = React.useMemo(() => ({
resolveSource
}), [resolveSource]);
// The context is intentionally `unknown`-generic; consumers narrow at the use
// site. Casting here is the one untyped seam for the provider's generics.
return /*#__PURE__*/_jsx(ChunkContext.Provider, {
value: value,
children: children
});
}