UNPKG

rwsdk

Version:

Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime

30 lines (29 loc) 1.29 kB
import React from "react"; import { ClientOnly } from "../client/client"; import { memoizeOnId } from "../lib/memoizeOnId"; // @ts-ignore import { useClientLookup } from "virtual:use-client-lookup.js"; export const loadModule = memoizeOnId(async (id) => { const moduleFn = useClientLookup[id]; if (!moduleFn) { throw new Error(`(client) No module found for '${id}' in module lookup for "use client" directive`); } return await moduleFn(); }); // context(justinvdm, 2 Dec 2024): re memoize(): React relies on the same promise instance being returned for the same id export const clientWebpackRequire = memoizeOnId(async (id) => { const [file, name] = id.split("#"); const promisedModule = loadModule(file); const promisedComponent = promisedModule.then((module) => module[name]); const didSSR = globalThis.__RWSDK_CONTEXT?.rw?.ssr; if (didSSR) { const awaitedComponent = await promisedComponent; return { [id]: awaitedComponent }; } const promisedDefault = promisedComponent.then((Component) => ({ default: Component, })); const Lazy = React.lazy(() => promisedDefault); const Wrapped = (props) => React.createElement(ClientOnly, null, React.createElement(Lazy, props)); return { [id]: Wrapped }; });