Skip to content

Commit 361d5a6

Browse files
committed
[Fizz] Recover from errors thrown by progressive enhancement form generation (#28611)
This a follow up to #28564. It's alternative to #28609 which takes #28610 into account. It used to be possible to return JSX from an action with `useActionState`. ```js async function action(errors, payload) { "use server"; try { ... } catch (x) { return <div>Error message</div>; } } ``` ```js const [errors, formAction] = useActionState(action); return <div>{errors}</div>; ``` Returning JSX from an action is itself not anything problematic. It's that it also has to return the previous state to the action reducer again that's the problem. When this happens we accidentally could serialize an Element back to the server. I fixed this in #28564 so it's now blocked if you don't have a temporary reference set. However, you can't have that for the progressive enhancement case. The reply is eagerly encoded as part of the SSR render. Typically you wouldn't have these in the initial state so the common case is that they show up after the first POST back that yields an error and it's only in the no-JS case where this happens so it's hard to discover. As noted in #28609 there's a security implication with allowing elements to be sent across this kind of payload, so we can't just make it work. When an error happens during SSR our general policy is to try to recover on the client instead. After all, SSR is mainly a perf optimization in React terms and it's not primarily intended for a no JS solution. This PR takes the approach that if we fail to generate the progressive enhancement payload. I.e. if the serialization of previous state / closures throw. Then we fallback to the replaying semantics just client actions instead which will succeed. The effect of this is that this pattern mostly just works: - First render in the typical case doesn't have any JSX in it so it just renders a progressive enhanced form. - If JS fails to hydrate or you click early we do a form POST. If that hits an error and it tries to render it using JSX, then the new page will render successfully - however this time with a Replaying form instead. - If you try to submit the form again it'll have to be using JS. Meaning if you use JSX as the error return value of form state and you make a first attempt that fails, then no JS won't work because either the first or second attempt has to hydrate. We have ideas for potentially optimizing away serializing unused arguments like if you don't actually use previous state which would also solve it but it wouldn't cover all cases such as if it was deeply nested in complex state. Another approach that I considered was to poison the prev state if you passed an element back but let it through to the action but if you try to render the poisoned value, it wouldn't work. The downside of this is when to error. Because in the progressive enhancement case it wouldn't error early but when you actually try to invoke it at which point it would be too late to fallback to client replaying. It would probably have to always error even on the client which is unfortunate since this mostly just works as long as it hydrates. DiffTrain build for [fee786a](fee786a)
1 parent 6e5eaa6 commit 361d5a6

10 files changed

+360
-221
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8ef14cf24219addedca3607dabb3bef37fb2e013
1+
fee786a057774ab687aff765345dd86fce534ab2

compiled/facebook-www/React-prod.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,4 +625,4 @@ exports.useSyncExternalStore = function (
625625
exports.useTransition = function () {
626626
return ReactCurrentDispatcher.current.useTransition();
627627
};
628-
exports.version = "18.3.0-www-modern-94e2c94e";
628+
exports.version = "18.3.0-www-modern-e739dc06";

compiled/facebook-www/React-profiling.modern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ exports.useSyncExternalStore = function (
629629
exports.useTransition = function () {
630630
return ReactCurrentDispatcher.current.useTransition();
631631
};
632-
exports.version = "18.3.0-www-modern-1d6a3285";
632+
exports.version = "18.3.0-www-modern-b2de0947";
633633
"undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ &&
634634
"function" ===
635635
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop &&

compiled/facebook-www/ReactDOMServer-dev.classic.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (__DEV__) {
1919
var React = require("react");
2020
var ReactDOM = require("react-dom");
2121

22-
var ReactVersion = "18.3.0-www-classic-c69e4b0a";
22+
var ReactVersion = "18.3.0-www-classic-1ff2dafc";
2323

2424
// This refers to a WWW module.
2525
var warningWWW = require("warning");
@@ -3170,12 +3170,45 @@ if (__DEV__) {
31703170
}
31713171

31723172
function pushAdditionalFormFields(target, formData) {
3173-
if (formData !== null) {
3173+
if (formData != null) {
31743174
// $FlowFixMe[prop-missing]: FormData has forEach.
31753175
formData.forEach(pushAdditionalFormField, target);
31763176
}
31773177
}
31783178

3179+
function getCustomFormFields(resumableState, formAction) {
3180+
var customAction = formAction.$$FORM_ACTION;
3181+
3182+
if (typeof customAction === "function") {
3183+
var prefix = makeFormFieldPrefix(resumableState);
3184+
3185+
try {
3186+
return formAction.$$FORM_ACTION(prefix);
3187+
} catch (x) {
3188+
if (
3189+
typeof x === "object" &&
3190+
x !== null &&
3191+
typeof x.then === "function"
3192+
) {
3193+
// Rethrow suspense.
3194+
throw x;
3195+
} // If we fail to encode the form action for progressive enhancement for some reason,
3196+
// fallback to trying replaying on the client instead of failing the page. It might
3197+
// work there.
3198+
3199+
{
3200+
// TODO: Should this be some kind of recoverable error?
3201+
error(
3202+
"Failed to serialize an action for progressive enhancement:\n%s",
3203+
x
3204+
);
3205+
}
3206+
}
3207+
}
3208+
3209+
return null;
3210+
}
3211+
31793212
function pushFormActionAttribute(
31803213
target,
31813214
resumableState,
@@ -3222,13 +3255,11 @@ if (__DEV__) {
32223255
}
32233256
}
32243257

3225-
var customAction = formAction.$$FORM_ACTION;
3258+
var customFields = getCustomFormFields(resumableState, formAction);
32263259

3227-
if (typeof customAction === "function") {
3260+
if (customFields !== null) {
32283261
// This action has a custom progressive enhancement form that can submit the form
32293262
// back to the server if it's invoked before hydration. Such as a Server Action.
3230-
var prefix = makeFormFieldPrefix(resumableState);
3231-
var customFields = formAction.$$FORM_ACTION(prefix);
32323263
name = customFields.name;
32333264
formAction = customFields.action || "";
32343265
formEncType = customFields.encType;
@@ -4088,13 +4119,11 @@ if (__DEV__) {
40884119
}
40894120
}
40904121

4091-
var customAction = formAction.$$FORM_ACTION;
4122+
var customFields = getCustomFormFields(resumableState, formAction);
40924123

4093-
if (typeof customAction === "function") {
4124+
if (customFields !== null) {
40944125
// This action has a custom progressive enhancement form that can submit the form
40954126
// back to the server if it's invoked before hydration. Such as a Server Action.
4096-
var prefix = makeFormFieldPrefix(resumableState);
4097-
var customFields = formAction.$$FORM_ACTION(prefix);
40984127
formAction = customFields.action || "";
40994128
formEncType = customFields.encType;
41004129
formMethod = customFields.method;

compiled/facebook-www/ReactDOMServer-dev.modern.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (__DEV__) {
1919
var React = require("react");
2020
var ReactDOM = require("react-dom");
2121

22-
var ReactVersion = "18.3.0-www-modern-ac7e39a5";
22+
var ReactVersion = "18.3.0-www-modern-bfd3973a";
2323

2424
// This refers to a WWW module.
2525
var warningWWW = require("warning");
@@ -3170,12 +3170,45 @@ if (__DEV__) {
31703170
}
31713171

31723172
function pushAdditionalFormFields(target, formData) {
3173-
if (formData !== null) {
3173+
if (formData != null) {
31743174
// $FlowFixMe[prop-missing]: FormData has forEach.
31753175
formData.forEach(pushAdditionalFormField, target);
31763176
}
31773177
}
31783178

3179+
function getCustomFormFields(resumableState, formAction) {
3180+
var customAction = formAction.$$FORM_ACTION;
3181+
3182+
if (typeof customAction === "function") {
3183+
var prefix = makeFormFieldPrefix(resumableState);
3184+
3185+
try {
3186+
return formAction.$$FORM_ACTION(prefix);
3187+
} catch (x) {
3188+
if (
3189+
typeof x === "object" &&
3190+
x !== null &&
3191+
typeof x.then === "function"
3192+
) {
3193+
// Rethrow suspense.
3194+
throw x;
3195+
} // If we fail to encode the form action for progressive enhancement for some reason,
3196+
// fallback to trying replaying on the client instead of failing the page. It might
3197+
// work there.
3198+
3199+
{
3200+
// TODO: Should this be some kind of recoverable error?
3201+
error(
3202+
"Failed to serialize an action for progressive enhancement:\n%s",
3203+
x
3204+
);
3205+
}
3206+
}
3207+
}
3208+
3209+
return null;
3210+
}
3211+
31793212
function pushFormActionAttribute(
31803213
target,
31813214
resumableState,
@@ -3222,13 +3255,11 @@ if (__DEV__) {
32223255
}
32233256
}
32243257

3225-
var customAction = formAction.$$FORM_ACTION;
3258+
var customFields = getCustomFormFields(resumableState, formAction);
32263259

3227-
if (typeof customAction === "function") {
3260+
if (customFields !== null) {
32283261
// This action has a custom progressive enhancement form that can submit the form
32293262
// back to the server if it's invoked before hydration. Such as a Server Action.
3230-
var prefix = makeFormFieldPrefix(resumableState);
3231-
var customFields = formAction.$$FORM_ACTION(prefix);
32323263
name = customFields.name;
32333264
formAction = customFields.action || "";
32343265
formEncType = customFields.encType;
@@ -4088,13 +4119,11 @@ if (__DEV__) {
40884119
}
40894120
}
40904121

4091-
var customAction = formAction.$$FORM_ACTION;
4122+
var customFields = getCustomFormFields(resumableState, formAction);
40924123

4093-
if (typeof customAction === "function") {
4124+
if (customFields !== null) {
40944125
// This action has a custom progressive enhancement form that can submit the form
40954126
// back to the server if it's invoked before hydration. Such as a Server Action.
4096-
var prefix = makeFormFieldPrefix(resumableState);
4097-
var customFields = formAction.$$FORM_ACTION(prefix);
40984127
formAction = customFields.action || "";
40994128
formEncType = customFields.encType;
41004129
formMethod = customFields.method;

0 commit comments

Comments
 (0)