Skip to content

Commit a1ace9d

Browse files
authored
Improve flagging of React.cache to remove indirection in bundled code (#28263)
Uses a better technique for conditionally disabling cache on the client
1 parent 91caa96 commit a1ace9d

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

packages/react/src/ReactCacheClient.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010
import {disableClientCache} from 'shared/ReactFeatureFlags';
1111
import {cache as cacheImpl} from './ReactCacheImpl';
1212

13-
export function cache<A: Iterable<mixed>, T>(fn: (...A) => T): (...A) => T {
14-
if (disableClientCache) {
15-
// On the client (i.e. not a Server Components environment) `cache` has
16-
// no caching behavior. We just return the function as-is.
17-
//
18-
// We intend to implement client caching in a future major release. In the
19-
// meantime, it's only exposed as an API so that Shared Components can use
20-
// per-request caching on the server without breaking on the client. But it
21-
// does mean they need to be aware of the behavioral difference.
22-
//
23-
// The rest of the behavior is the same as the server implementation — it
24-
// returns a new reference, extra properties like `displayName` are not
25-
// preserved, the length of the new function is 0, etc. That way apps can't
26-
// accidentally depend on those details.
27-
return function () {
28-
// $FlowFixMe[incompatible-call]: We don't want to use rest arguments since we transpile the code.
29-
return fn.apply(null, arguments);
30-
};
31-
} else {
32-
return cacheImpl(fn);
33-
}
13+
export function noopCache<A: Iterable<mixed>, T>(fn: (...A) => T): (...A) => T {
14+
// On the client (i.e. not a Server Components environment) `cache` has
15+
// no caching behavior. We just return the function as-is.
16+
//
17+
// We intend to implement client caching in a future major release. In the
18+
// meantime, it's only exposed as an API so that Shared Components can use
19+
// per-request caching on the server without breaking on the client. But it
20+
// does mean they need to be aware of the behavioral difference.
21+
//
22+
// The rest of the behavior is the same as the server implementation — it
23+
// returns a new reference, extra properties like `displayName` are not
24+
// preserved, the length of the new function is 0, etc. That way apps can't
25+
// accidentally depend on those details.
26+
return function () {
27+
// $FlowFixMe[incompatible-call]: We don't want to use rest arguments since we transpile the code.
28+
return fn.apply(null, arguments);
29+
};
3430
}
31+
32+
export const cache: typeof noopCache = disableClientCache
33+
? noopCache
34+
: cacheImpl;

0 commit comments

Comments
 (0)