[Flight] Preserve location of bound server actions (#30778)
Follow up to #30741. We need to keep the location of the action when it's bound so we can still jump to it.
Sebastian Markbåge committed
Aug 22, 2024 at 12:36 UTC
eb7570bc1631243af8df21c8a87173cc12f2d880
3 files changed
+85
-19
packages/react-server-dom-esm/src/ReactFlightESMReferences.js
+31
-7
@@ -52,14 +52,38 @@ function bind(this: ServerReference<any>): any {
52
// $FlowFixMe[unsupported-syntax]
53
const newFn = FunctionBind.apply(this, arguments);
54
if (this.$$typeof === SERVER_REFERENCE_TAG) {
55
- // $FlowFixMe[method-unbinding]
55
+ if (__DEV__) {
56
+ const thisBind = arguments[0];
57
+ if (thisBind != null) {
58
+ console.error(
59
+ 'Cannot bind "this" of a Server Action. Pass null or undefined as the first argument to .bind().',
60
+ );
61
+ }
62
+ }
63
const args = ArraySlice.call(arguments, 1);
57
- return Object.defineProperties((newFn: any), {
58
- $$typeof: {value: SERVER_REFERENCE_TAG},
59
- $$id: {value: this.$$id},
60
- $$bound: {value: this.$$bound ? this.$$bound.concat(args) : args},
61
- bind: {value: bind},
62
- });
64
+ const $$typeof = {value: SERVER_REFERENCE_TAG};
65
+ const $$id = {value: this.$$id};
66
+ const $$bound = {value: this.$$bound ? this.$$bound.concat(args) : args};
67
+ return Object.defineProperties(
68
+ (newFn: any),
69
+ __DEV__
70
+ ? {
71
+ $$typeof,
72
+ $$id,
73
+ $$bound,
74
+ $$location: {
75
+ value: this.$$location,
76
+ configurable: true,
77
+ },
78
+ bind: {value: bind, configurable: true},
79
+ }
80
+ : {
81
+ $$typeof,
82
+ $$id,
83
+ $$bound,
84
+ bind: {value: bind, configurable: true},
85
+ },
86
+ );
87
}
88
return newFn;
89
}
packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js
+31
-6
@@ -66,13 +66,38 @@ function bind(this: ServerReference<any>): any {
66
// $FlowFixMe[unsupported-syntax]
67
const newFn = FunctionBind.apply(this, arguments);
68
if (this.$$typeof === SERVER_REFERENCE_TAG) {
69
+ if (__DEV__) {
70
+ const thisBind = arguments[0];
71
+ if (thisBind != null) {
72
+ console.error(
73
+ 'Cannot bind "this" of a Server Action. Pass null or undefined as the first argument to .bind().',
74
+ );
75
+ }
76
+ }
77
const args = ArraySlice.call(arguments, 1);
70
- return Object.defineProperties((newFn: any), {
71
- $$typeof: {value: SERVER_REFERENCE_TAG},
72
- $$id: {value: this.$$id},
73
- $$bound: {value: this.$$bound ? this.$$bound.concat(args) : args},
74
- bind: {value: bind},
75
- });
78
+ const $$typeof = {value: SERVER_REFERENCE_TAG};
79
+ const $$id = {value: this.$$id};
80
+ const $$bound = {value: this.$$bound ? this.$$bound.concat(args) : args};
81
+ return Object.defineProperties(
82
+ (newFn: any),
83
+ __DEV__
84
+ ? {
85
+ $$typeof,
86
+ $$id,
87
+ $$bound,
88
+ $$location: {
89
+ value: this.$$location,
90
+ configurable: true,
91
+ },
92
+ bind: {value: bind, configurable: true},
93
+ }
94
+ : {
95
+ $$typeof,
96
+ $$id,
97
+ $$bound,
98
+ bind: {value: bind, configurable: true},
99
+ },
100
+ );
101
}
102
return newFn;
103
}
packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js
+23
-6
@@ -75,12 +75,29 @@ function bind(this: ServerReference<any>): any {
75
}
76
}
77
const args = ArraySlice.call(arguments, 1);
78
- return Object.defineProperties((newFn: any), {
79
- $$typeof: {value: SERVER_REFERENCE_TAG},
80
- $$id: {value: this.$$id},
81
- $$bound: {value: this.$$bound ? this.$$bound.concat(args) : args},
82
- bind: {value: bind},
83
- });
78
+ const $$typeof = {value: SERVER_REFERENCE_TAG};
79
+ const $$id = {value: this.$$id};
80
+ const $$bound = {value: this.$$bound ? this.$$bound.concat(args) : args};
81
+ return Object.defineProperties(
82
+ (newFn: any),
83
+ __DEV__
84
+ ? {
85
+ $$typeof,
86
+ $$id,
87
+ $$bound,
88
+ $$location: {
89
+ value: this.$$location,
90
+ configurable: true,
91
+ },
92
+ bind: {value: bind, configurable: true},
93
+ }
94
+ : {
95
+ $$typeof,
96
+ $$id,
97
+ $$bound,
98
+ bind: {value: bind, configurable: true},
99
+ },
100
+ );
101
}
102
return newFn;
103
}