Skip to content

streamRevalidatedTags

streamRevalidatedTags returns a streaming Response that pushes revalidated cache tags to a connected client over a long-lived connection. Service workers and clients subscribe to this stream to learn when tagged resources become stale, so they can refresh cached data immediately (see Caching Strategies).

import { streamRevalidatedTags } from "@toapi/server";
import { api } from "./api";
export const GET = () => streamRevalidatedTags({ cache: api.cache });
function streamRevalidatedTags(options: { cache: Cache }): Response;

Type: Cache

The cache / pub-sub instance whose invalidations should be streamed. Pass the same instance that your API uses — api.cache — so subscribers receive the tags invalidated by your mutation handlers.

  • On connect, the handler assigns a random client ID and stores it in a session cookie (__tapi-session, HttpOnly; SameSite=Strict).
  • It subscribes to the cache’s pub/sub. Whenever tags are invalidated, they are written to the stream as a newline-terminated, space-separated list (e.g. books book-42\n).
  • Invalidations that originated from this same client (matched by client ID) are skipped, so a client never receives an echo of its own mutations.
  • A keepalive newline is sent every 10 seconds to hold the connection open.
  • When the connection is cancelled, the keepalive interval is cleared and the subscription is removed.

The response is sent with Content-Type: text/tapi-tags.