Unverified Commit 45890f6e authored by Joseph's avatar Joseph Committed by GitHub

docs: note that non-function exports of a use cache file are not available to…

docs: note that non-function exports of a use cache file are not available to Client Components (#100594)

In a file with `'use cache'` at the top, Client Components can import
the exported functions, but other exports such as constants and objects
are dropped from what the client receives. Importing one fails the build
with `Export X doesn't exist in target module`, and the docs didn't say
so. This adds a Good to know bullet to the `use cache` API reference
that states the restriction and the fix: move shared values to a
separate file without the directive.

The client-side drop is covered by the compiler test case
`crates/next-custom-transforms/tests/fixture/server-actions/client-graph/18`.
The build error was reproduced on 16.4.2 and 16.5.0-canary.23 with
Turbopack production builds.

- f3a47ec2154 docs: note that non-function exports of a use cache file
are not available to Client Components
parent 8e47c2ef
...@@ -607,6 +607,7 @@ Framework function exports are covered like any other, so [`generateMetadata`](/ ...@@ -607,6 +607,7 @@ Framework function exports are covered like any other, so [`generateMetadata`](/
> **Good to know**: > **Good to know**:
> >
> - When a cached directive (`use cache`, [`use cache: private`](/docs/app/api-reference/directives/use-cache-private), or [`use cache: remote`](/docs/app/api-reference/directives/use-cache-remote)) is at the top of a file, you can import its exported functions into a Client Component and call them directly; they run on the server and return the result, similar to a [Server Function](/docs/app/glossary#server-function). Prefer calling cached functions on the server and passing results down as props. > - When a cached directive (`use cache`, [`use cache: private`](/docs/app/api-reference/directives/use-cache-private), or [`use cache: remote`](/docs/app/api-reference/directives/use-cache-remote)) is at the top of a file, you can import its exported functions into a Client Component and call them directly; they run on the server and return the result, similar to a [Server Function](/docs/app/glossary#server-function). Prefer calling cached functions on the server and passing results down as props.
> - Only exported functions are available to Client Components. Other exports, such as constants and objects, are dropped from what the client receives, so importing one fails the build with an "export doesn't exist" error. Move shared values to a separate file without the directive.
### Caching a route segment's output with `use cache` ### Caching a route segment's output with `use cache`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment