Improve flagging of `React.cache` to remove indirection in bundled code (#28263)
Uses a better technique for conditionally disabling cache on the client
Josh Story committed
Feb 6, 2024 at 17:40 UTC
a1ace9d3c2a0fc53703f11e30f32d0a009c5a8c9
1 file changed
+21
-21
packages/react/src/ReactCacheClient.js
+21
-21
@@ -10,25 +10,25 @@
10
import {disableClientCache} from 'shared/ReactFeatureFlags';
11
import {cache as cacheImpl} from './ReactCacheImpl';
12
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
+ };
30
}
31
+
32
+export const cache: typeof noopCache = disableClientCache
33
+ ? noopCache
34
+ : cacheImpl;