@@ -10,6 +10,7 @@ const compress = require('compression');
1010const chalk = require ( 'chalk' ) ;
1111const express = require ( 'express' ) ;
1212const http = require ( 'http' ) ;
13+ const React = require ( 'react' ) ;
1314
1415const { renderToPipeableStream} = require ( 'react-dom/server' ) ;
1516const { createFromNodeStream} = require ( 'react-server-dom-esm/client' ) ;
@@ -62,23 +63,39 @@ app.all('/', async function (req, res, next) {
6263 if ( req . accepts ( 'text/html' ) ) {
6364 try {
6465 const rscResponse = await promiseForData ;
65-
6666 const moduleBaseURL = '/src' ;
6767
6868 // For HTML, we're a "client" emulator that runs the client code,
6969 // so we start by consuming the RSC payload. This needs the local file path
7070 // to load the source files from as well as the URL path for preloads.
71- const root = await createFromNodeStream (
72- rscResponse ,
73- moduleBasePath ,
74- moduleBaseURL
75- ) ;
71+
72+ let root ;
73+ let Root = ( ) => {
74+ if ( root ) {
75+ return React . use ( root ) ;
76+ }
77+
78+ return React . use (
79+ ( root = createFromNodeStream (
80+ rscResponse ,
81+ moduleBasePath ,
82+ moduleBaseURL
83+ ) )
84+ ) ;
85+ } ;
7686 // Render it into HTML by resolving the client components
7787 res . set ( 'Content-type' , 'text/html' ) ;
78- const { pipe} = renderToPipeableStream ( root , {
79- // TODO: bootstrapModules inserts a preload before the importmap which causes
80- // the import map to be invalid. We need to fix that in Float somehow.
81- // bootstrapModules: ['/src/index.js'],
88+ const { pipe} = renderToPipeableStream ( React . createElement ( Root ) , {
89+ importMap : {
90+ imports : {
91+ react : 'https://esm.sh/react@experimental?pin=v124&dev' ,
92+ 'react-dom' : 'https://esm.sh/react-dom@experimental?pin=v124&dev' ,
93+ 'react-dom/' : 'https://esm.sh/react-dom@experimental&pin=v124&dev/' ,
94+ 'react-server-dom-esm/client' :
95+ '/node_modules/react-server-dom-esm/esm/react-server-dom-esm-client.browser.development.js' ,
96+ } ,
97+ } ,
98+ bootstrapModules : [ '/src/index.js' ] ,
8299 } ) ;
83100 pipe ( res ) ;
84101 } catch ( e ) {
@@ -89,6 +106,7 @@ app.all('/', async function (req, res, next) {
89106 } else {
90107 try {
91108 const rscResponse = await promiseForData ;
109+
92110 // For other request, we pass-through the RSC payload.
93111 res . set ( 'Content-type' , 'text/x-component' ) ;
94112 rscResponse . on ( 'data' , data => {
0 commit comments