@@ -46,7 +46,7 @@ const {readFile} = require('fs').promises;
46
46
47
47
const React = require ( 'react' ) ;
48
48
49
- async function renderApp ( res , returnValue ) {
49
+ async function renderApp ( res , returnValue , formState ) {
50
50
const { renderToPipeableStream} = await import (
51
51
'react-server-dom-webpack/server'
52
52
) ;
@@ -93,13 +93,13 @@ async function renderApp(res, returnValue) {
93
93
React . createElement ( App ) ,
94
94
] ;
95
95
// For client-invoked server actions we refresh the tree and return a return value.
96
- const payload = returnValue ? { returnValue, root } : root ;
96
+ const payload = { root , returnValue, formState } ;
97
97
const { pipe} = renderToPipeableStream ( payload , moduleMap ) ;
98
98
pipe ( res ) ;
99
99
}
100
100
101
101
app . get ( '/' , async function ( req , res ) {
102
- await renderApp ( res , null ) ;
102
+ await renderApp ( res , null , null ) ;
103
103
} ) ;
104
104
105
105
app . post ( '/' , bodyParser . text ( ) , async function ( req , res ) {
@@ -108,6 +108,7 @@ app.post('/', bodyParser.text(), async function (req, res) {
108
108
decodeReply,
109
109
decodeReplyFromBusboy,
110
110
decodeAction,
111
+ decodeFormState,
111
112
} = await import ( 'react-server-dom-webpack/server' ) ;
112
113
const serverReference = req . get ( 'rsc-action' ) ;
113
114
if ( serverReference ) {
@@ -139,7 +140,7 @@ app.post('/', bodyParser.text(), async function (req, res) {
139
140
// We handle the error on the client
140
141
}
141
142
// Refresh the client and return the value
142
- renderApp ( res , result ) ;
143
+ renderApp ( res , result , null ) ;
143
144
} else {
144
145
// This is the progressive enhancement case
145
146
const UndiciRequest = require ( 'undici' ) . Request ;
@@ -153,12 +154,14 @@ app.post('/', bodyParser.text(), async function (req, res) {
153
154
const action = await decodeAction ( formData ) ;
154
155
try {
155
156
// Wait for any mutations
156
- await action ( ) ;
157
+ const result = await action ( ) ;
158
+ const formState = decodeFormState ( result , formData ) ;
159
+ renderApp ( res , null , formState ) ;
157
160
} catch ( x ) {
158
161
const { setServerState} = await import ( '../src/ServerState.js' ) ;
159
162
setServerState ( 'Error: ' + x . message ) ;
163
+ renderApp ( res , null , null ) ;
160
164
}
161
- renderApp ( res , null ) ;
162
165
}
163
166
} ) ;
164
167
0 commit comments