Schemas that transform
Send cached values whose schemas transform, such as dates.
When you send the cache, 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's nothing to think about. For a schema that
transforms, such as a Schema.decodeTo that maps a response to your own
type (Responses in other shapes), it means
the transformation must encode as well as decode.
The examples import todos.ts from Getting started.
A transformation that can't encode, 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;
useSuspenseQuerywaits for that fetch while React hydrates;- a plain
useQuerystarts 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, since a
response's other fields are ignored when decoding, and encode then 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, or its schema can't
// encode it, or there's no schema.
export const = .(, { : }).;