@@ -17,28 +17,36 @@ declare global {
1717type RSCServerRootProps = {
1818 componentName : string ;
1919 componentProps : Record < string , unknown > ;
20- }
20+ } ;
2121
2222if ( ! ( 'use' in React && typeof React . use === 'function' ) ) {
23- throw new Error ( 'React.use is not defined. Please ensure you are using React 18 with experimental features enabled or React 19+ to use server components.' ) ;
23+ throw new Error (
24+ 'React.use is not defined. Please ensure you are using React 18 with experimental features enabled or React 19+ to use server components.' ,
25+ ) ;
2426}
2527
2628const { use } = React ;
2729
28- const createFromReactOnRailsNodeStream = ( stream : NodeJS . ReadableStream , ssrManifest : Record < string , unknown > ) => {
30+ const createFromReactOnRailsNodeStream = (
31+ stream : NodeJS . ReadableStream ,
32+ ssrManifest : Record < string , unknown > ,
33+ ) => {
2934 const transformedStream = transformRSCStream ( stream ) ;
3035 return createFromNodeStream ( transformedStream , ssrManifest ) ;
31- }
36+ } ;
3237
33- const createSSRManifest = async ( reactServerManifestFileName : string , reactClientManifestFileName : string ) => {
38+ const createSSRManifest = async (
39+ reactServerManifestFileName : string ,
40+ reactClientManifestFileName : string ,
41+ ) => {
3442 const [ reactServerManifest , reactClientManifest ] = await Promise . all ( [
3543 loadJsonFile ( reactServerManifestFileName ) ,
3644 loadJsonFile ( reactClientManifestFileName ) ,
3745 ] ) ;
3846
3947 const ssrManifest = {
4048 moduleLoading : {
41- prefix : " /webpack/development/" ,
49+ prefix : ' /webpack/development/' ,
4250 crossOrigin : null ,
4351 } ,
4452 moduleMap : { } as Record < string , unknown > ,
@@ -51,38 +59,47 @@ const createSSRManifest = async (reactServerManifestFileName: string, reactClien
5159 id : ( serverFileBundlingInfo as { id : string } ) . id ,
5260 chunks : ( serverFileBundlingInfo as { chunks : string [ ] } ) . chunks ,
5361 name : '*' ,
54- }
62+ } ,
5563 } ;
5664 } ) ;
5765
5866 return ssrManifest ;
59- }
67+ } ;
6068
61- const RSCServerRoot : RenderFunction = async ( { componentName, componentProps } : RSCServerRootProps , railsContext ?: RailsContext ) => {
62- if ( ! railsContext ?. serverSide || ! railsContext ?. reactClientManifestFileName || ! railsContext ?. reactServerClientManifestFileName ) {
69+ const RSCServerRoot : RenderFunction = async (
70+ { componentName, componentProps } : RSCServerRootProps ,
71+ railsContext ?: RailsContext ,
72+ ) => {
73+ if (
74+ ! railsContext ?. serverSide ||
75+ ! railsContext ?. reactClientManifestFileName ||
76+ ! railsContext ?. reactServerClientManifestFileName
77+ ) {
6378 throw new Error (
64- `${ 'serverClientManifestFileName and reactServerClientManifestFileName are required. ' +
65- 'Please ensure that React Server Component webpack configurations are properly set ' +
66- 'as stated in the React Server Component tutorial. The received rails context is: ' } ${ JSON . stringify ( railsContext ) } `
79+ `${
80+ 'serverClientManifestFileName and reactServerClientManifestFileName are required. ' +
81+ 'Please ensure that React Server Component webpack configurations are properly set ' +
82+ 'as stated in the React Server Component tutorial. The received rails context is: '
83+ } ${ JSON . stringify ( railsContext ) } `,
6784 ) ;
6885 }
6986
7087 if ( typeof generateRSCPayload !== 'function' ) {
7188 throw new Error (
7289 'generateRSCPayload is not defined. Please ensure that you are using at least version 4.0.0 of ' +
73- 'React on Rails Pro and the node renderer, and that ReactOnRailsPro.configuration.enable_rsc_support ' +
74- 'is set to true.'
90+ 'React on Rails Pro and the node renderer, and that ReactOnRailsPro.configuration.enable_rsc_support ' +
91+ 'is set to true.' ,
7592 ) ;
7693 }
7794
7895 const ssrManifest = await createSSRManifest (
7996 railsContext . reactServerClientManifestFileName ,
80- railsContext . reactClientManifestFileName
97+ railsContext . reactClientManifestFileName ,
8198 ) ;
8299 const rscPayloadStream = await generateRSCPayload (
83100 componentName ,
84101 componentProps ,
85- railsContext . serverSideRSCPayloadParameters
102+ railsContext . serverSideRSCPayloadParameters ,
86103 ) ;
87104
88105 // Tee the stream to pass it to the server component and the payload container
@@ -92,20 +109,14 @@ const RSCServerRoot: RenderFunction = async ({ componentName, componentProps }:
92109 rscPayloadStream . pipe ( rscPayloadStream2 ) ;
93110 const serverComponentElement = createFromReactOnRailsNodeStream ( rscPayloadStream1 , ssrManifest ) ;
94111
95- return ( ) => React . createElement (
96- React . Fragment ,
97- null , [
98- React . createElement (
99- React . Fragment ,
100- { key : 'serverComponentElement' } ,
101- use ( serverComponentElement )
102- ) ,
103- React . createElement (
104- RSCPayloadContainer ,
105- { RSCPayloadStream : rscPayloadStream2 , key : 'rscPayloadContainer' } ,
106- ) ,
107- ]
108- ) ;
112+ return ( ) =>
113+ React . createElement ( React . Fragment , null , [
114+ React . createElement ( React . Fragment , { key : 'serverComponentElement' } , use ( serverComponentElement ) ) ,
115+ React . createElement ( RSCPayloadContainer , {
116+ RSCPayloadStream : rscPayloadStream2 ,
117+ key : 'rscPayloadContainer' ,
118+ } ) ,
119+ ] ) ;
109120} ;
110121
111122export default RSCServerRoot ;
0 commit comments