Skip to content

Commit cd633db

Browse files
committed
Preserve location of bound server actions
1 parent e831c23 commit cd633db

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

packages/react-server-dom-esm/src/ReactFlightESMReferences.js

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,38 @@ function bind(this: ServerReference<any>): any {
5252
// $FlowFixMe[unsupported-syntax]
5353
const newFn = FunctionBind.apply(this, arguments);
5454
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+
}
5663
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+
);
6387
}
6488
return newFn;
6589
}

packages/react-server-dom-turbopack/src/ReactFlightTurbopackReferences.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,38 @@ function bind(this: ServerReference<any>): any {
6666
// $FlowFixMe[unsupported-syntax]
6767
const newFn = FunctionBind.apply(this, arguments);
6868
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+
}
6977
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+
);
76101
}
77102
return newFn;
78103
}

packages/react-server-dom-webpack/src/ReactFlightWebpackReferences.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,29 @@ function bind(this: ServerReference<any>): any {
7575
}
7676
}
7777
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+
);
84101
}
85102
return newFn;
86103
}

0 commit comments

Comments
 (0)