Read: loading pages, caches and embeds
load.ts: how a ref or URL becomes a DocPage, the module caches and generations, redirects, embed cards and query blocks.

src/read/load.ts builds the DocPage a document screen renders. It owns the module-level caches that keep every document on the stack answered instantly: "going Back never flashes. Failures are never cached" (L1-L2). Rows for listings, the directory and author pages live in the same file and are described in Rows, directory and author pages.

Shapes

DocPage (L34-L51): ref (exactly what was requested, "" for the home, or a whole URL), server (the server it was actually read from), uid, path (joined with /), title, summary, icon, error, authors: Person[] (first 5), date (UTC, like Nov 9, 2026), at (the same instant in ms, 0 when unknown), blocks: DocBlock[], children: DocItem[], dirHref, shareUrl ({server}/hm/{uid}/{path}). The Contract mirror is in read.contract.

Entry points

Export

Behaviour

loadDocs(server, refs)

Dedupes refs and loads each with loadDoc, in order. Called by the readDocs source.

loadDoc(server, ref)

Cache hit, else join the in-flight request, else buildPage. A rejection becomes failedPage (uncached).

resolveLink(server, url)

A deep link to a ref: hm and gateway URLs directly; a site page through that site's home account. null when not a document link.

siteHomeUid(server)

registeredAccountUid from /hm/api/config, cached per server.

formatDate, dateMs

{seconds, nanos}, ISO string, Date or ms to a UTC date string or epoch ms.

forget, forgetAll

Drop a server's caches, see below.

buildPage step by step

buildPage:

    If the ref is a URL (linkOfRef), switch server to that URL's origin when it is http(s) ("A typed web address is read from its own server, never silently from the default"), then resolveLink. Failure: This link is not a Hypermedia document.

    An empty ref means the site home: siteHomeUid(server). Failure: This server has no registered site.

    fetchDocument calls Resource with id=hm://… and follows type: "redirect" answers (redirectTarget as a string or {id}), up to 10 hops with a visited set. The page then uses the destination's uid and path for children, discussion and sharing.

    Inline mentions (mentionLinks) are named: an account through the cached Account lookup, a document through ResourceMetadata. Failures leave the name empty.

    blocksToRows produces the rows and records embeds and queries in pending.

    In parallel: each embed becomes a card (embedCard), each query block loads its items (queryItems), the child list loads (childItems) and the first 5 authors are looked up.

Embed cards fetch the target document (one Resource call, redirects included). With a block fragment only that block's subtree is summarised. The summary is the document's metadata.summary or the first 240 characters of plain text. A failed embed shows This document is not available. with the alert symbol; a failed query shows Couldn't load this list. Neither fails the page.

queryItems sends the block's own query object to QueryBlock, drops private results, caps at query.limit or 50, and takes author names from accountsMetadata and comment counts from interactionSummaries[id].comments, so no extra request is made per row.

childItems returns nothing when a Query block in the document already lists the document's own children (hasQueryTargetingSelf). Otherwise it calls Query with mode: "Children" and drops children the content already links to.

Document errors

Message

When

This document doesn't exist on this server.

Resource answered not-found

This document is unavailable ({type}).

any other non-document type

This document has a redirect loop or too many redirects.

revisited ref or 10 hops

This link is not a document.

no uid could be derived

Couldn't load this document.

an error with no message

Client errors pass through as they are. failedPage keeps ref and the link's own server, with empty uid for URL refs, so the screen never substitutes the default home.

Caches and generations

    docCache: key {server}|{ref}, value DocPage.

    inflight: key {server}|{ref}, value the pending promise (dedupes concurrent asks).

    homeCache: key {server}, value the home account uid.

    metaCache: key {server}|acct:{uid}, value {name, icon}.

    dirCache and authorCache: key {server}|{uid}, see listings.

generations holds a counter per server (L95-L104). Every loader records the generation when it starts and writes to a cache only if it is unchanged when the answer arrives. forget(server) bumps the counter and deletes that server's entries, so a late response from before a pull to refresh can be returned to its caller but can never repopulate a cache. This is the module half of the pattern in State, refresh and stale-reply patterns.

Inferred from the keys: a document opened by URL is cached under the configured server's key, while its account names and home uid are cached under its own server, which a refresh of the configured server does not bump.

Tests

    load.test.ts: 7 tests. Late responses after refresh, per-server epochs, redirects and redirect cycles, a failed child listing staying retryable, author retry.

    deep-links.test.ts: link resolution, the URL kept as ref, typed sites read from their own server, an unreachable typed site.

    cards.test.ts: a query block needs no extra requests.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime