Capítulo 75 de 116

Chapter 75: cacheSignal

Core Idea

cacheSignal() returns an AbortSignal tied to the lifetime of the current server-render cache (Ch 74), letting cached async work know when it's safe to abort because the cache it belongs to is being torn down.

Key Concepts

  • Paired with cache: exists specifically to support cleanup for work memoized via cache — when the request-scoped cache is discarded (the render finished or was aborted), the signal fires so any still-in-flight cached operation can cancel itself instead of continuing pointlessly.
  • Standard AbortSignal semantics: since it's a real AbortSignal, it composes with any API that already accepts one (e.g. fetch(url, { signal: cacheSignal() })) — no React-specific cancellation protocol to learn.
  • Server-only, RSC-specific: like cache, this is part of the React Server Components server-rendering surface, not something used in Client Components.
  • Prevents wasted work: without this, a slow cached fetch could keep running (and consuming resources) even after the render that needed it has already ended — wiring the signal in lets that work actually stop.

Code Examples

const getUser = cache(async (id) => {
  return fetch(`/api/users/${id}`, { signal: cacheSignal() }).then(r => r.json());
});
  • What it demonstrates: passing the cache-scoped abort signal directly into fetch, so the request cancels automatically if the surrounding cache is torn down before it resolves.

Key Takeaways

  1. Use this to make cached async work properly cancellable, not just memoized.
  2. It's a standard AbortSignal — plug it into any cancellation-aware API the same way you would any other abort signal.
  3. Relevant specifically inside functions wrapped with cache, in a Server Components context.

Connects To

  • Ch 74 (cache): the memoization mechanism this signal's lifetime is tied to.
  • Ch 110 (Server Components): the rendering context both this and cache belong to.