@@ -10,6 +10,7 @@ const compress = require('compression');
10
10
const chalk = require ( 'chalk' ) ;
11
11
const express = require ( 'express' ) ;
12
12
const http = require ( 'http' ) ;
13
+ const React = require ( 'react' ) ;
13
14
14
15
const { renderToPipeableStream} = require ( 'react-dom/server' ) ;
15
16
const { createFromNodeStream} = require ( 'react-server-dom-esm/client' ) ;
@@ -62,23 +63,39 @@ app.all('/', async function (req, res, next) {
62
63
if ( req . accepts ( 'text/html' ) ) {
63
64
try {
64
65
const rscResponse = await promiseForData ;
65
-
66
66
const moduleBaseURL = '/src' ;
67
67
68
68
// For HTML, we're a "client" emulator that runs the client code,
69
69
// so we start by consuming the RSC payload. This needs the local file path
70
70
// 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
+ } ;
76
86
// Render it into HTML by resolving the client components
77
87
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' ] ,
82
99
} ) ;
83
100
pipe ( res ) ;
84
101
} catch ( e ) {
@@ -89,6 +106,7 @@ app.all('/', async function (req, res, next) {
89
106
} else {
90
107
try {
91
108
const rscResponse = await promiseForData ;
109
+
92
110
// For other request, we pass-through the RSC payload.
93
111
res . set ( 'Content-type' , 'text/x-component' ) ;
94
112
rscResponse . on ( 'data' , data => {
0 commit comments