Skip to content

fix(client): a server render answers every read an on-demand collection makes - #62

Merged
grrowl merged 10 commits into
mainfrom
fix/ssr-answers-every-read
Nov 17, 2026
Merged

grrowl merged 10 commits into
mainfrom
fix/ssr-answers-every-read

Conversation

@grrowl

@grrowl grrowl commented Nov 17, 2026

Copy link
Copy Markdown
Owner

A server render of an on-demand collection failed whenever one of its queries was answered by a fetch. SsrSnapshotTransport rejected every fetch with SsrReadOnlyError, so the query went to error.

That happened in three ordinary cases:

  • a query that an earlier query covers, for example a list and a detail row of one table;
  • on @tanstack/db 0.9 and later, a single query with orderBy and limit, because core sends extra requests for the full source, for ties, and for an underfilled window;
  • a window that grows during the render.

What changed

  • A render refuses writes, never reads. A fetch without a cursor is answered with one snapshot read. A fetch with a cursor is answered with the two reads the server makes: the rows tied at the boundary, then the next page. Every read joins the resume cursor of the render. There is no server change and no change to the read request.
  • A fetched page writes each key once. The two reads of a cursor fetch happen at two positions in a render, so a row that moves past the boundary between them is in both. On 1.0 that failed the load with a duplicate key error. The first copy now wins, and the catch-up after hydration brings the newer value. On the WebSocket path the server makes both reads in one turn and cannot return a key twice, so nothing changes there.
  • A cursor that lacks either half is refused, as the server refuses it.

Things to know

On 0.9 and later, core asks for every matching row when a limited query is ordered by text with the default sort, or when the first page ends on a NULL in the order column. A render now reads and ships those rows, as the browser already loads them over the socket. Top 10 of 100,000 rows under the default sort dehydrates 100,000. With stringSort: "lexical" it dehydrates 10. The README's SSR section has the warning.

A request the server refuses fails the render, where the socket answers it with no rows.

Verification

  • Red tests first, in three rounds. They drive the cases through the adapter and the real Durable Object, including dehydrate and hydrate with writes between the reads.
  • An adversarial review by a different model, in two rounds, with its own runs in workerd on 1.0.0-rc.1.5, 0.9.2 and 0.8.6. The first round found a cursor fetch inside a first load that still failed, and the unstated read size. The second compared ten cursor frames between the socket and the render, ran eight more races through hydration, and reproduced the read sizes at 100,000 rows. Its verdict was mergeable.
  • Typecheck and the full suite pass on 1.0.0-rc.1.5 and 0.8.6. The SSR test files pass on 0.9.2.

Not run: the full suite on 0.9.2, a numeric order column, and examples/ssr, which uses an eager collection and so never reached this path.

The decisions are recorded as a dated amendment to ADR-0011 and a note in ADR-0003.

🤖 Generated with Claude Code

grrowl and others added 10 commits November 17, 2026 14:53
A request that an earlier watch covers loads with a cursor-less fetch,
and SsrSnapshotTransport rejects every fetch, so the covered query goes
to error in the render. Pins both mount orders, a single ordered limited
query (core sends it covered requests on 0.9 and later), the dehydrate
and hydrate round trip with the min cursor, and that a cursor fetch
still fails loud.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A request that an open or pending watch covers loads its rows with a
cursor-less fetch (ADR-0023 D2). SsrSnapshotTransport rejected every
fetch, so in a server render a covered query went to error, and on
@tanstack/db 0.9 and later so did a single orderBy + limit query.

The snapshot transport now answers a fetch without a cursor with one
snapshot read of the same where/orderBy/limit; its position joins the
render's MIN cursor. A cursor fetch (load more) still rejects with
SsrReadOnlyError. The adapter and the WebSocket transport are unchanged.
Recorded as a dated amendment to ADR-0011.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A render refuses writes, never reads. Pins a cursor request inside a
first load (0.9 and later), a window that moves during the render (a
cursor request on 0.8.x), the row that a render's two reads of one
cursor request both return, and a cursor request whose reads have writes
between them, through dehydrate and hydrate. Closes the review's test
gaps: a covered query whose row only its fetch brings, a covered
fetch's own orderBy, a default sort window that only the full-source
request corrects, a tie request, and two collections on one transport.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…eads

Core sends a cursor request inside the first load of an ordered, limited
query (0.9 and later, with an index and an order it can express as a
cursor, when the window is underfilled), so refusing it failed renders
the browser serves. SsrSnapshotTransport now reads the boundary ties
and(where, whereCurrent) without a limit, then the page
and(where, whereFrom) with it, through the existing read. Both join the
MIN cursor. No change to the read request and no server change.

A row that moves past the boundary between the two reads is in both;
the next commit writes it once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A render reads the two halves of a cursor request at two positions, so
one row can be in both, and a second sync insert of the key fails the
load on @tanstack/db 1.0. installPage now skips a key written earlier
in the same page: insert-if-absent inside the page as well as against
held rows. The first copy wins; the hydration catch-up from the MIN
cursor brings the newer value. A server page never repeats a key, so
the WebSocket path is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rewrites the ADR-0011 amendment of 2026-11-17: every fetch is answered,
the two-position reasoning for a cursor request and both of its races,
the rows a render may read (measured: top 10 of 100 000 under the
default sort reads 100 010 and dehydrates 100 000 on 0.9 and later;
11 and 10 with stringSort lexical), what an app sees for a refused
predicate on each side, and the rejected options. Dates insert-if-absent
inside one page in ADR-0003. README: the SSR rule and a warning about
ordered queries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The server refuses it as a malformed cursor; the snapshot transport
read the whole table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The server refuses it as a malformed cursor because the ties read would
be an unbounded scan; the snapshot transport read the whole table. It
now rejects loud, as a render does for every refused read. The adapter
never sends such a frame; SsrSnapshotTransport is public.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
README: the full read also follows a NULL at the end of the first page
(any type, any sort) and reads a boundary tie group whole; a fetch is
one or two reads; a refused request is the one difference from the
socket. ADR-0011: the malformed cursor refusal, a third two-position
effect (a row read early and deleted later is still rendered), and the
cursor shapes for which a server page never repeats a key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…on makes

Merges the SSR fix: a fetch without a cursor is one snapshot read, a fetch
with a cursor is the two reads the server makes, and a fetched page writes
each key once. Adds the changelog entries.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@grrowl
grrowl merged commit 4399e08 into main Nov 17, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant