Skip to Content
Effetch Query
Server rendering

Load the cache in the browser

Load the cache the server sent into the browser's client.

This page finishes the three steps of Send the cache: the browser render that picks up what the server sent.

The examples import client.ts from Getting started, and todo-list.tsx from Send the cache.

3. Render the same tree under a HydrationBoundary. Give it what the server sent:

import {  } from "@monarcode/effetch-query";
import {
  ,
  ,
} from "@monarcode/effetch-query/react";
import {  } from "react-dom/client";

import {  } from "./client.js";
import {  } from "./todo-list.js";

const  = .(
  .("query-state")?. ?? "[]",
) as .;

const  = .("root");
if ( !== null) {
  (
    ,
    < ={}>
      < ={}>
        < />
      </>
    </>,
  );
}

The first render shows the server's values, so it matches the server's HTML, and nothing is fetched until they're stale.

A boundary can sit anywhere, such as one per route, given each page's state as you navigate. It loads into the client of the nearest QueryProvider, and throws a MissingQueryProviderError without one. How it loads depends on what's cached:

  • A query that isn't cached loads during render, so the children read it in the same pass.
  • A query already cached takes the new value only if it's newer. It loads after commit, so a transition that brings new state doesn't change the page it leaves until React shows the new one.
  • A query the server was still loading waits for its result instead of fetching (Stream the cache).
  • A value its schema can't decode is dropped, and the query fetches.

Under a QueryProvider whose resetKey changes, a boundary loads only what the server dehydrated after the change, as Sign out and switch accounts explains.

Without React. Call QueryClient.hydrate(client, state) before reading the queries:

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

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

const  = .(
  .("query-state")?. ?? "[]",
) as .;

.(, );

hydrate follows the same rules: a cached query takes only newer data, and a query the server was still loading waits for its result. A value its schema can't decode is dropped, and the query fetches. It also loads state from Effect's own Hydration.dehydrate, for other atoms.