@ogygia/content · Docs sites
Remote functions
remotes(site) turns a site's sidebar, shell data, pages and search into Kit remote functions, so pages and layouts read the site without importing the corpus. They're prerendered by default, and each page's body crosses as HTML the server already rendered.
On this page
The site lives in a server module, because it holds the whole compiled corpus. Pages and layouts can’t import a server module, yet they need the site’s data. remotes() is the bridge: it wraps the site’s read functions as Kit remote functions, which a page can import and await. The browser only ever sees what those functions return.
Every site that keeps its corpus on the server needs them. The other shape, a site in an ordinary module that pages import directly, skips remotes and pays for it with a warning that the corpus could leak.
import { remotes } from '@ogygia/content/server';
import { docs } from './docs.server';
export const { nav, meta, page, search } = remotes(docs, {
base: '/docs'
});Setup
Call remotes(site, options?) from @ogygia/content/server in a .remote.ts file. It imports $app/server, which only works there, and Kit requires every export of the file to be a remote function, so export only what it returns. They need Kit’s kit.experimental.remoteFunctions, Svelte’s compilerOptions.experimental.async and ogygia’s transport hook.
base is the mount prefix built into every href the remotes return. It defaults to '' and doesn’t read the site’s own base (#2282), so pass it here. The shell’s base prop and docs.search need it too; Mount it on a route lists the three places.
Remote list
| Remote | Calls | Returns |
|---|---|---|
meta(slug?) | docs.meta({ base, slug }) | { nav, switcher, data }, everything DocsShell needs, in one call. |
nav(slug?) | docs.nav({ base, slug }) | The sidebar tree only, for chrome of your own. |
page(slug) | docs.page(slug, { base }) | One page view with its body rendered, or null for an unknown address. |
search(q) | docs.search(q, { base }) | Ranked hits. See Search. |
The slug passed to meta and nav picks the version or language on a site with dimensions; with none, or '', they serve the default one. On a site without dimensions the slug changes nothing, so call meta() with no argument and every page shares one stored answer. Every argument must be a string (or nothing, for meta and nav); anything else is refused before the site is read.
Use meta for a shell: it’s one await and one stored answer that carries the tree, the switcher and data.
Body HTML
A page’s body is a held region: a component waiting to render, which can’t be sent as it is. The page remote awaits it on the server, which renders it to HTML and turns it into a small ticket that carries the HTML. It also drops the entry’s source, the server-only reader of the raw file. What crosses is the page view’s data and the ticket, never the corpus.
<Doc> renders the ticket with <Region>. Islands the .svx file imported wake from it as they would anywhere, so each page ships only its own HTML and the code of its own islands, however big the corpus. Render a page and its shell shows the page component. Its ! after the await is safe when the route exports docs.load, which answers a 404 for an unknown address before the page renders. Import the remotes as a namespace (* as docs) when the file also uses page from $app/state.
Prerendering
modes picks how each remote runs:
'prerender'(the default) makes a Kitprerenderremote. Its answers are computed at build time, once per distinct argument, and served as static files. It’s made withdynamic: true, so on a server it still answers an argument the build didn’t see. It’s required when the pages that await it are prerendered.'query'makes a Kitqueryremote that reads the site on every request, for content that changes between deploys, such as a live source.
modes.nav sets both nav and meta, and modes.page sets page. search is always a query, since every search is different; for a fully static site, ship the index to the browser instead and don’t export search.
The remotes always read the public view: they pass no read context, even in 'query' mode, so a draft that only a preview may see stays hidden (#2282). A preview site writes its own page remote that passes the context; Show drafts to some visitors has one.
import { remotes } from '@ogygia/content/server';
import { docs } from './docs.server';
export const { meta, page } = remotes(docs, {
base: '/docs',
modes: { nav: 'query', page: 'query' }
});Do and don’t
- Do create the remotes in a
.remote.tsfile and import only those from pages and layouts. That’s what keeps the corpus out of the browser. - Do pass
basetoremotes(), even when the site has one. - Do keep the default
'prerender'mode unless the content really changes per request. Static answers are faster and cost nothing to serve. - Don’t return a raw
entry.bodyfrom a remote or load of your own. It fails withheld_region_plain_import; await it first, aspagedoes.