[]previous and next page

ResourceCache

Bridge between resources and the server/hydration runtime.

Bridge between resources and the server/hydration runtime. During server rendering it collects in-flight fetches and replays finished ones on the next pass; during hydration it serves the values the server already fetched, so the client does not fetch them again.

Import#

ts
import type { ResourceCache } from "@shadow-js/core";

Signature#

ts
interface ResourceCache {
  /**
   * Deterministic key for a resource created at this point of the render. `explicit` is the
   * resource's `key` option, or `undefined` when it has none.
   */
  key(explicit: string | undefined): string;
  /** A cached value, wrapped so that `undefined` values are representable. */
  read(key: string): { value: unknown } | undefined;
  /**
   * A fetch started; the cache stores its value under `key` when it resolves. `options.deferStream` is
   * true for a resource that holds the first streamed flush (see `ResourceOptions.deferStream`).
   */
  track(key: string, promise: Promise<unknown>, options?: ResourceTrackOptions): void;
  /**
   * The fetch an earlier pass of the same render started for `key` and that has not settled, when there
   * is one. A resource that finds it waits for that fetch instead of starting another, so a pass that
   * renders again does not fetch twice. Optional: a cache that does not keep earlier fetches omits it.
   */
  pending?(key: string): Promise<unknown> | undefined;
}

Members#

Name Type Description
key key(explicit: string | undefined): string Deterministic key for a resource created at this point of the render. explicit is the resource's key option, or undefined when it has none.
read read(key: string): { value: unknown } | undefined A cached value, wrapped so that undefined values are representable.
track track(key: string, promise: Promise<unknown>, options?: ResourceTrackOptions): void A fetch started; the cache stores its value under key when it resolves. options.deferStream is true for a resource that holds the first streamed flush (see ResourceOptions.deferStream).
pending? pending?(key: string): Promise<unknown> | undefined The fetch an earlier pass of the same render started for key and that has not settled, when there is one. A resource that finds it waits for that fetch instead of starting another, so a pass that renders again does not fetch twice. Optional: a cache that does not keep earlier fetches omits it.