Skip to Content
Effetch Query
Server rendering

Stream the cache

Send the page before every query has loaded, and stream each one as it finishes.

Some queries take longer than the rest of the page. Rather than hold the whole page for them, the server can send it at once and stream each query to the browser as it finishes. With pending: "promise", QueryClient.dehydrate sends a query still loading as a promise of its result. The browser waits for it instead of fetching, and Suspense shows the query when it lands:

import {  } from "@monarcode/effetch-query";

declare const : .;

export const  = .(, { : "promise" });

This page builds on Send the cache. The examples import api.ts and todos.ts from Getting started.

Where a promise can go

A promise doesn't survive JSON.stringify, so the renderToString recipe in Send the cache can't stream. Use pending: "promise" where your framework streams promises to the browser, as React's server components, TanStack Start's and React Router's serializers do. Each page in Frameworks works this way.

Start a prefetch without awaiting it

To stream a query, start its fetch and don't wait for it, then dehydrate with pending: "promise". The page renders at once, and the value follows:

import {  } from "@monarcode/effetch-query";

import {  } from "./todos.js";

declare const : .;

// Starts the fetch, and doesn't wait for it.
void .(());

export const  = .(, { : "promise" });

prefetch never rejects, so a prefetch cut short when the client is disposed isn't an unhandled rejection. Load a query that may redirect with client.fetch instead, as Redirect from a query explains.

Queries started while the server renders

Only a query that has started when dehydrate runs is sent. In a server render, useSuspenseQuery fetches a query the cache doesn't have and suspends on it. If you dehydrated before the render, that query isn't sent, and the browser fetches it again.

Load what the page reads before you dehydrate, or dehydrate after the render has started the rest. The TanStack Router integration does this for you: it sends each query as it starts.

When a streamed query fails

A promise settles when the server's fetch does:

  • A success goes to the browser, which shows it.
  • A failure goes with its error if you send failures (failures: "include"). Otherwise the promise settles with nothing, and the browser fetches the query itself.
  • A client disposed first settles the promise with nothing, and the browser fetches the query itself.

Dispose the client after the response

Keep the client until its promises settle. On a server that answers with a streamed Response, the request's handler returns before the streamed results are sent, so it can't dispose the client there. QueryClient.disposeAfter(client, response) wraps the response's body so that the client is disposed after the body has been sent:

import * as  from "effect/ManagedRuntime";

import {  } from "@monarcode/effetch-query";

import {  } from "./api.js";

// Your app's server render, which streams the page.
declare const : (: .) => Promise<Response>;

const  = .();

export const  = async (): Promise<Response> => {
  const  = .({ :  });
  return .(, await ());
};

The client is disposed when the body ends, fails, or the reader cancels it, as when a browser disconnects. disposeAfter suits React Router's middleware, Hono, Bun.serve and Deno.serve, and React Router shows it in use. A few things to know about it:

  • Send the response it returns. One that's discarded is never read, so the client is never disposed.
  • Only the status, status text and headers are kept, not a runtime's own options (Cloudflare's encodeBody, say). The body goes out as a stream, so a fixed body loses the length the runtime would have sent.
  • A response with no body disposes the client at once. A body already read or locked throws, and the client is disposed all the same.
  • A failed dispose() is logged with console.error, not thrown.
  • The core uses ReadableStream and Response only when it's called, so it still loads where they don't exist, such as React Native.

What's next

On this page