react/scripts
Sebastian Markbåge e1dc03492e
Expose cacheSignal() alongside cache() (#33557)
This was really meant to be there from the beginning. A `cache()`:ed
entry has a life time. On the server this ends when the render finishes.
On the client this ends when the cache of that scope gets refreshed.

When a cache is no longer needed, it should be possible to abort any
outstanding network requests or other resources. That's what
`cacheSignal()` gives you. It returns an `AbortSignal` which aborts when
the cache lifetime is done based on the same execution scope as a
`cache()`ed function - i.e. `AsyncLocalStorage` on the server or the
render scope on the client.

```js
import {cacheSignal} from 'react';
async function Component() {
  await fetch(url, { signal: cacheSignal() });
}
```

For `fetch` in particular, a patch should really just do this
automatically for you. But it's useful for other resources like database
connections.

Another reason it's useful to have a `cacheSignal()` is to ignore any
errors that might have triggered from the act of being aborted. This is
just a general useful JavaScript pattern if you have access to a signal:

```js
async function getData(id, signal) {
  try {
     await queryDatabase(id, { signal });
  } catch (x) {
     if (!signal.aborted) {
       logError(x); // only log if it's a real error and not due to cancellation
     }
     return null;
  }
}
```

This just gets you a convenient way to get to it without drilling
through so a more idiomatic code in React might look something like.

```js
import {cacheSignal} from "react";

async function getData(id) {
  try {
     await queryDatabase(id);
  } catch (x) {
     if (!cacheSignal()?.aborted) {
       logError(x);
     }
     return null;
  }
}
```

If it's called outside of a React render, we normally treat any cached
functions as uncached. They're not an error call. They can still load
data. It's just not cached. This is not like an aborted signal because
then you couldn't issue any requests. It's also not like an infinite
abort signal because it's not actually cached forever. Therefore,
`cacheSignal()` returns `null` when called outside of a React render
scope.

Notably the `signal` option passed to `renderToReadableStream` in both
SSR (Fizz) and RSC (Flight Server) is not the same instance that comes
out of `cacheSignal()`. If you abort the `signal` passed in, then the
`cacheSignal()` is also aborted with the same reason. However, the
`cacheSignal()` can also get aborted if the render completes
successfully or fatally errors during render - allowing any outstanding
work that wasn't used to clean up. In the future we might also expand on
this to give different
[`TaskSignal`](https://developer.mozilla.org/en-US/docs/Web/API/TaskSignal)
to different scopes to pass different render or network priorities.

On the client version of `"react"` this exposes a noop (both for
Fiber/Fizz) due to `disableClientCache` flag but it's exposed so that
you can write shared code.
2025-06-17 17:04:40 -04:00
..
babel [flags] remove enableRemoveConsolePatches (#32425) 2025-02-24 10:00:22 -05:00
bench Don't minify symbols in production builds (#28881) 2024-04-20 11:23:46 -04:00
ci [ci] Try to parallelize devtools builds (#32266) 2025-01-30 11:49:04 -05:00
devtools fix[scripts/devtools/publish-release]: parse version list instead of handling 404 (#31087) 2024-09-30 17:07:54 +01:00
error-codes Expose cacheSignal() alongside cache() (#33557) 2025-06-17 17:04:40 -04:00
eslint [Codemod] Update copyright header to Meta (#25315) 2022-10-18 11:19:24 -04:00
eslint-rules [tests] Remove to*Dev matchers (#31989) 2025-01-07 14:17:14 -05:00
flags Add --cleanup option to flags script to show groups of flags by status (#31762) 2024-12-13 15:49:06 -05:00
flow [Flight] Track Debug Info from Synchronously Unwrapped Promises (#33485) 2025-06-11 12:07:10 -04:00
git Remove leftover env variable logic in pre-commit hook 2015-09-01 14:35:47 -07:00
jest Revert Node Web Streams (#33472) 2025-06-06 16:26:36 -04:00
perf-counters [Codemod] Update copyright header to Meta (#25315) 2022-10-18 11:19:24 -04:00
prettier [prettier] Combine compiler and runtime configs 2024-06-21 12:05:29 -04:00
print-warnings Disable consoleWithStackDev Transform except in RN/WWW (#30313) 2024-07-12 14:39:38 -04:00
react-compiler [compiler]: fix link compiler & 4 broken tests from path containing spaces (#33409) 2025-06-09 08:40:27 -07:00
release [release] Don't lookup build-info.json when updating version numbers (#32778) 2025-03-28 14:35:29 -04:00
rollup Revert Node Web Streams (#33472) 2025-06-06 16:26:36 -04:00
shared [Flight] Add Web Stream support to the Flight Client in Node (#33473) 2025-06-06 17:14:15 -04:00
tasks [ci] Cleanup more references to circleci 2024-07-29 19:18:03 -04:00