@@ -26,6 +26,20 @@ const createFromFetch = async (fetchPromise: Promise<Response>) => {
2626 return createFromReadableStream < React . ReactNode > ( transformedStream ) ;
2727} ;
2828
29+ /**
30+ * Fetches an RSC payload via HTTP request.
31+ *
32+ * This function:
33+ * 1. Serializes the component props
34+ * 2. Makes an HTTP request to the RSC payload generation endpoint
35+ * 3. Processes the response stream into React elements
36+ *
37+ * This is used for client-side navigation or when rendering components
38+ * that weren't part of the initial server render.
39+ *
40+ * @param props - Object containing component name, props, and railsContext
41+ * @returns A Promise resolving to the rendered React element
42+ */
2943const fetchRSC = ( { componentName, componentProps, railsContext } : ClientGetReactServerComponentProps ) => {
3044 const propsString = JSON . stringify ( componentProps ) ;
3145 const { rscPayloadGenerationUrl } = railsContext ;
@@ -65,12 +79,48 @@ const createRSCStreamFromArray = (payloads: string[]) => {
6579 return stream ;
6680} ;
6781
82+ /**
83+ * Creates React elements from preloaded RSC payloads in the page.
84+ *
85+ * This function:
86+ * 1. Creates a ReadableStream from the array of payload chunks
87+ * 2. Transforms the stream to handle console logs and other processing
88+ * 3. Uses React's createFromReadableStream to process the payload
89+ *
90+ * This is used during hydration to avoid making HTTP requests when
91+ * the payload is already embedded in the page.
92+ *
93+ * @param payloads - Array of RSC payload chunks from the global array
94+ * @returns A Promise resolving to the rendered React element
95+ */
6896const createFromPreloadedPayloads = ( payloads : string [ ] ) => {
6997 const stream = createRSCStreamFromArray ( payloads ) ;
7098 const transformedStream = transformRSCStreamAndReplayConsoleLogs ( stream ) ;
7199 return createFromReadableStream < React . ReactNode > ( transformedStream ) ;
72100} ;
73101
102+ /**
103+ * Fetches and renders a server component on the client side.
104+ *
105+ * This function:
106+ * 1. Checks for embedded RSC payloads in window.REACT_ON_RAILS_RSC_PAYLOADS
107+ * 2. If found, uses the embedded payload to avoid an HTTP request
108+ * 3. If not found (during client navigation or dynamic rendering), fetches via HTTP
109+ * 4. Processes the RSC payload into React elements
110+ *
111+ * The embedded payload approach ensures optimal performance during initial page load,
112+ * while the HTTP fallback enables dynamic rendering after navigation.
113+ *
114+ * @param componentName - Name of the server component to render
115+ * @param componentProps - Props to pass to the server component
116+ * @param railsContext - Context for the current request
117+ * @returns A Promise resolving to the rendered React element
118+ *
119+ * @important This is an internal function. End users should not use this directly.
120+ * Instead, use the useRSC hook which provides getComponent and getCachedComponent functions
121+ * for fetching or retrieving cached server components. For rendering server components,
122+ * consider using RSCRoute component which handles the rendering logic automatically.
123+ */
74124const getReactServerComponent = ( {
75125 componentName,
76126 componentProps,
0 commit comments