11import * as React from 'react' ;
22import { createFromNodeStream } from 'react-on-rails-rsc/client.node' ;
33import { PassThrough } from 'stream' ;
4- import type { RenderFunction , RailsContext } from './types' ;
5- import transformRSCStream from './transformRSCNodeStream' ;
6- import loadJsonFile from './loadJsonFile' ;
7- import RSCPayloadContainer from './RSCPayloadContainer' ;
4+ import type { RenderFunction , RailsContext } from './types/index.ts' ;
5+ import transformRSCStream from './transformRSCNodeStream.ts' ;
6+ import loadJsonFile from './loadJsonFile.ts' ;
7+ import RSCPayloadContainer from './RSCPayloadContainer.tsx' ;
8+ import { ensureReactUseAvailable } from './reactApis.cts' ;
9+
10+ ensureReactUseAvailable ( ) ;
811
912declare global {
1013 function generateRSCPayload (
@@ -19,12 +22,6 @@ type RSCServerRootProps = {
1922 componentProps : Record < string , unknown > ;
2023} ;
2124
22- if ( ! ( 'use' in React && typeof React . use === 'function' ) ) {
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- ) ;
26- }
27-
2825const { use } = React ;
2926
3027const createFromReactOnRailsNodeStream = (
@@ -40,24 +37,25 @@ const createSSRManifest = async (
4037 reactClientManifestFileName : string ,
4138) => {
4239 const [ reactServerManifest , reactClientManifest ] = await Promise . all ( [
43- loadJsonFile ( reactServerManifestFileName ) ,
44- loadJsonFile ( reactClientManifestFileName ) ,
40+ loadJsonFile ( reactServerManifestFileName ) as Promise < Record < string , { id : string ; chunks : string [ ] } > > ,
41+ loadJsonFile ( reactClientManifestFileName ) as Promise < Record < string , { id : string } > > ,
4542 ] ) ;
4643
4744 const ssrManifest = {
48- moduleLoading : {
49- prefix : '/webpack/development/' ,
50- crossOrigin : null ,
51- } ,
45+ // The `moduleLoading` property is utilized by the React runtime to load JavaScript modules.
46+ // It can accept options such as `prefix` and `crossOrigin` to specify the path and crossorigin attribute for the modules.
47+ // In our case, since the server code is bundled into a single bundle, there is no need to load additional JavaScript modules.
48+ // As a result, we set this property to an empty object because it will not be used.
49+ moduleLoading : { } ,
5250 moduleMap : { } as Record < string , unknown > ,
5351 } ;
5452
5553 Object . entries ( reactClientManifest ) . forEach ( ( [ aboluteFileUrl , clientFileBundlingInfo ] ) => {
56- const serverFileBundlingInfo = reactServerManifest [ aboluteFileUrl ] ;
57- ssrManifest . moduleMap [ ( clientFileBundlingInfo as { id : string } ) . id ] = {
54+ const { id , chunks } = reactServerManifest [ aboluteFileUrl ] ;
55+ ssrManifest . moduleMap [ clientFileBundlingInfo . id ] = {
5856 '*' : {
59- id : ( serverFileBundlingInfo as { id : string } ) . id ,
60- chunks : ( serverFileBundlingInfo as { chunks : string [ ] } ) . chunks ,
57+ id,
58+ chunks,
6159 name : '*' ,
6260 } ,
6361 } ;
0 commit comments