Skip to Content
Effetch Query
Server rendering

What's sent

Which queries QueryClient.dehydrate sends to the browser, and what it leaves out.

When you send the cache, QueryClient.dehydrate encodes each cached query as JSON. Most queries cross without any work. When one doesn't, the browser fetches a query the server already had, or dehydrate throws. This page shows what decides it: schemas, keys and failures, and the options dehydrate takes.

The examples import todos.ts from Getting started.

Queries with a schema

Each query with a schema is sent, encoded with it. An Effetch endpoint's output is its schema; an endpoint without an output isn't sent. A query of your own needs a schema, as Queries from any Effect shows.

Some queries are left out, with no error, and the browser fetches them itself:

  • a query with no schema;
  • a value its schema can't encode, such as a schema that transforms one way only, as Schemas that transform shows;
  • a value whose schema needs a service to encode, which an Effetch endpoint's output may: dehydrate runs outside any Effect, so it has no services to give;
  • a placeholder, which is never cached;
  • a query still loading with nothing to show yet, unless you stream the cache;
  • a failed query, unless you send failures.

A stale query that's refetching is sent with the value it has. On the server, Query.prefetch runs a stream query to its first element, which dehydrate sends when the query has a schema.

Schemas that transform

A value crosses to the browser in its wire shape. dehydrate encodes it with the query's schema, from the decoded value back to what the server sent, and the browser decodes it with the same schema. For a plain Schema.Struct that takes no thought. Effect's date schemas, such as Schema.Date and Schema.DateFromString, encode too: a date goes as an ISO string.

A schema you write with Schema.decodeTo is different. It maps a response to your own type, as Responses in other shapes does, and its transformation must encode as well as decode. One that can't, such as one whose encode is SchemaGetter.forbidden, makes dehydrate leave the query out, with no error. The page still renders on the server, but the browser has no value for that query:

  • the browser fetches it again, a second request for data the server already had;
  • useSuspenseQuery waits for that fetch while React hydrates;
  • a plain useQuery starts out pending in the browser, though the server rendered the data, which can show a loading state or a hydration mismatch.

So write encode as the mirror of decode. It doesn't have to rebuild the whole response, only a wire value that decode turns back into the same value. Declare only the fields you read in the wire schema: a response's other fields are ignored when decoding, so encode has only those to write:

import * as  from "effect/Schema";
import * as  from "effect/SchemaTransformation";

// The API sends amounts in minor units, under its own names.
const  = .({
  : .,
  : .,
});

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

export const  = .(
  .(
    ,
    .({
      : () => ({ : ., : . / 100 }),
      : () => ({
        : .,
        : .(. * 100),
      }),
    }),
  ),
);

The same goes for an entity's copies that a write or a sync put into a query: they're sent encoded with that query's own schema.

To check that a query is sent, dehydrate it on its own while you build the page:

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

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

declare const : .;

// 0 when no todos query is sent: none is cached, it's still loading or
// failed, its schema can't encode it, or there's no schema.
export const  = .(, { :  }).;

Keys on the server

The browser finds each query by its key, so dehydrate turns keys into strings. Two errors come from keys. dehydrate throws them only for a query it would send:

  • QueryClient.NonJsonKeyError when a key holds something JSON can't carry. Its segment is the first such value. Keys can't hold:
    • a Date or a class instance;
    • undefined in an array;
    • NaN or Infinity;
    • a function, a bigint or a symbol.
  • QueryClient.DuplicateQueryKeyError when two cached queries have one key, such as two api.query of one Effetch endpoint. The browser couldn't tell them apart. Give one of them another key, or a prefix: an Effetch builder takes one with fromClient(ApiClient, { key: [...] }).

Both have a _tag to match on. An endpoint with a query schema encodes its query values before they go in the key, so a date there becomes a string. See Keys from a query schema.

Send failures

By default a failed query is left out, so server errors never reach the page, and the browser fetches it again. To send failures, pass failures: "include":

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

declare const : .;

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

A failed query then goes with its error, encoded with the query's schema.error, and a defect as Schema.Defect. A query of your own with no schema.error isn't sent when it fails.

In the browser, a component that reads the failure with useQuery shows it, then retries it as it mounts, unless the query's retryOnMount is false. A useSuspenseQuery reader throws it to the nearest error boundary, as Suspense and error boundaries explains.

An Effetch endpoint's errors go as follows:

  • an HttpError goes without its headers, but with its request URL and response text, so keep secrets out of the URLs your server calls;
  • a NetworkError goes without its cause, and a DecodeError without its issue;
  • the errors of statusErrors go as their schemas encode them;
  • a query that failed with a plugin's error isn't sent.

fromClient reads the client's defaultError and statusErrors from a Fetch.Service class. A client of another kind needs them given, as the next section shows.

A client that isn't a Fetch.Service class

This section is for you if you made your Effetch client by hand, with Fetch.make and a Context.Service key, rather than declaring it with Fetch.Service. It matters when you send failures, or read a family's error schema.

To send a failure, Effetch Query needs the schemas the client decodes errors with: its defaultError and its statusErrors. The defaultError is also each family's error schema (todo.error), for endpoints without an error schema of their own.

A Fetch.Service class carries both, so fromClient reads them from it. A client made with Fetch.make and provided through a Context.Service key doesn't: its config is there only after its layer is built, which is after your queries are defined.

Keep the client's config in one object. Make the client from it, and give fromClient the same two fields:

import * as  from "effect/Context";
import type * as  from "effect/Effect";
import * as  from "effect/Schema";

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

class  extends .<>()(
  "SessionExpired",
  { : . },
) {}

const  = {
  : "https://billing.example.com",
  : .({ : . }),
  : { 401:  },
};

export const  = .();

export class  extends .<
  ,
  .<typeof >
>()("BillingClient") {}

declare module "@monarcode/effetch-query" {
  interface Register {
    readonly : ;
  }
}

export const  = (, {
  : ["billing"],
  : .,
  : .,
});

export const  = .(
  .("GET /invoices", { : .(.) }),
);
// invoices() is the query keyed ["billing", "invoices"]
export const  = .(["billing"]);

The sample also uses key: ["billing"], so its queries can't clash with another backend's, and Query.refetch(["billing"]) reaches all of them.

If you leave the two fields out, the client still decodes your errors, and they're still typed. With failures: "include", two things change:

  • Without statusErrors, a query that failed with one of those errors isn't sent, so the browser fetches it again.
  • Without defaultError, an HttpError's body goes as plain JSON, so a query whose error body isn't JSON isn't sent. A family's error is the endpoint's own error schema, or nothing.

On a Fetch.Service class, the same two options override what the class was declared with. You need that only when the class's layer builds its client from a different config.

dehydrate options

QueryClient.dehydrate takes the following options as its second argument:

OptionWhat it doesDefault
queriesA target: only the queries it matches are sent, as Query.invalidate matches them. queries: ["todos"] sends those under ["todos"].Every query
pendingA query still loading, with nothing to show yet. "promise" sends a promise of its result, as Stream the cache shows; "omit" leaves it out, and the browser fetches it."omit"
failuresA failed query. "include" sends it with its error; "omit" leaves it out, and the browser fetches it."omit"

What's next

  • Stream the cache: send queries still loading as promises.
  • Query keys: how keys are built, and how a target matches them.
  • Query errors: what a failure holds, and how to turn a status into an error of your own.
  • Frameworks: send the cache in TanStack Start, Next.js and React Router.

On this page