Skip to content

Commit

Permalink
Remove client caching from cache API
Browse files Browse the repository at this point in the history
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. `cache(fn)` will 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.
  • Loading branch information
acdlite committed Jan 16, 2024
1 parent d855463 commit 0438f3b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
19 changes: 19 additions & 0 deletions packages/react/src/ReactCacheClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* 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.
return fn;
}
File renamed without changes.
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

0 comments on commit 0438f3b

Please sign in to comment.