Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove client caching from cache API
We haven't yet decided how we want `cache` to work on the client. The
lifetime of the cache is more complex than on the server, where it only
has to live as long as a single request.

Since it's more important to ship this on the server, we're removing the
existing behavior from the client for now. On the client (i.e. not a
Server Components environment) `cache` will have not have any caching
behavior.

The rest of the behavior is the same as the server implementation — it
returns a new reference, extra properties like `displayName` are not
preserved, the length of the new function is 0, etc. That way apps can't
accidentally depend on those details.

We intend to implement client caching in a future major release. In the
meantime, it's only exposed as an API so that Shared Components can use
per-request caching on the server without breaking on the client.
  • Loading branch information
acdlite committed Jan 17, 2024
commit 6b66aedfd492c8a5617c3d213fa146b51fa96ecc
21 changes: 21 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactCache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,25 @@ describe('ReactCache', () => {
expect(x).toBe(y);
expect(z).not.toBe(x);
});

// @gate enableCache
it('introspection of returned wrapper function is same on client and server', async () => {
// When the variant flag is true, test the client version of `cache`.
if (gate(flags => flags.variant)) {
jest.resetModules();
jest.mock('react', () => jest.requireActual('react'));
const ClientReact = require('react');
cache = ClientReact.cache;
}

function foo(a, b, c) {
return a + b + c;
}
foo.displayName = 'Custom display name';

const cachedFoo = cache(foo);
expect(cachedFoo).not.toBe(foo);
expect(cachedFoo.length).toBe(0);
expect(cachedFoo.displayName).toBe(undefined);
});
});
27 changes: 27 additions & 0 deletions packages/react/src/ReactCacheClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (c) Meta Platforms, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

export function cache<A: Iterable<mixed>, T>(fn: (...A) => T): (...A) => T {
// On the client (i.e. not a Server Components environment) `cache` has
// no caching behavior. We just return the function as-is.
//
// We intend to implement client caching in a future major release. In the
// meantime, it's only exposed as an API so that Shared Components can use
// per-request caching on the server without breaking on the client. But it
// does mean they need to be aware of the behavioral difference.
//
// The rest of the behavior is the same as the server implementation — it
// returns a new reference, extra properties like `displayName` are not
// preserved, the length of the new function is 0, etc. That way apps can't
// accidentally depend on those details.
return function () {
// $FlowFixMe[incompatible-call]: We don't want to use rest arguments since we transpile the code.
return fn.apply(null, arguments);
};
}
2 changes: 1 addition & 1 deletion packages/react/src/ReactClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {createContext} from './ReactContext';
import {lazy} from './ReactLazy';
import {forwardRef} from './ReactForwardRef';
import {memo} from './ReactMemo';
import {cache} from './ReactCache';
import {cache} from './ReactCacheClient';
import {postpone} from './ReactPostpone';
import {
getCacheSignal,
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/ReactServer.experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
import {forwardRef} from './ReactForwardRef';
import {lazy} from './ReactLazy';
import {memo} from './ReactMemo';
import {cache} from './ReactCache';
import {cache} from './ReactCacheServer';
import {startTransition} from './ReactStartTransition';
import {postpone} from './ReactPostpone';
import version from 'shared/ReactVersion';
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/ReactServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
import {forwardRef} from './ReactForwardRef';
import {lazy} from './ReactLazy';
import {memo} from './ReactMemo';
import {cache} from './ReactCache';
import {cache} from './ReactCacheServer';
import {startTransition} from './ReactStartTransition';
import version from 'shared/ReactVersion';

Expand Down