@ogygia/content · Docs sites
Docs sites
site() turns one or more collections into a whole site, with a sidebar, prev/next links, search, a sitemap, llms.txt and a shell around each page. It runs on the server and hands the page plain data, so the corpus never reaches the browser.
On this page
A collection knows which entries exist. Once you have more than a handful of them, you also need to decide how they read as a site: what goes in the sidebar and in what order, which page comes next, what search finds, what a crawler or a language model sees. site() handles all of that from the collections you already have.
You give site() your collections and an arrangement (the outline), and it gives back a set of read functions: one for the sidebar tree, one for a page, one for search, one for the prerender list, and so on. Each one returns plain data. The page and the shell render that data, and the compiled corpus stays on the server.
This docs site runs on site(), and so does the Playground ↗: a second site inside this one with four markdown corpora, an OpenAPI reference, a blog, a version and language switcher, and nine themes. If all you need is one page with one body, a collection on its own is enough; reach for site() when you want the site around many pages.
import { content, links, site } from '@ogygia/content';
const guides = content({
loader: import.meta.og.loader.folder('../content/docs')
});
export const docs = site({
outline: guides,
checks: [links()]
});Scaffold
npx ogygia site init writes a working docs site into your app as ordinary files: a collection and its site(), two starter pages, the page route, a layout with the shell, a +layout.ts (csr = false, prerender = true), a home page, an error page, the search, sitemap, llms.txt and raw markdown routes, and a search page that works without JavaScript. Docs site lists every file, and site init every flag.
The scaffold takes the short path. Its collection is a markdown loader placed in one Docs group, rather than the bare folder collection below. It keeps the site in src/lib/docs.ts, an ordinary module, and its page and layout import it directly, so every body renders in the page’s own server pass with no remote functions. That works on csr = false routes, but the build warns with content_collection_not_server_only, because nothing stops a client module from importing the corpus. The rest of this page builds the other shape, where the site lives in a server module and pages read it through remotes. Move the scaffold to it when the warning matters to you.
npx ogygia site init --layout src/routes/docs/+layout.svelteSite definition
Put the collections and the site() call in a .server.ts module, or a file under src/lib/server/. Kit refuses to bundle those for the browser, so the corpus can’t leak into a client chunk (Collections explains why that matters).
A bare collection as the outline arranges itself: a folder collection’s NN- prefixes give the order and its +meta.json files name the sections. Outline covers arranging several collections by hand.
data holds facts about the site. All three fields are required when you pass it: title and description head llms.txt, and origin is the absolute origin for the sitemap, llms.txt and feeds. It wins over the request’s origin, which a prerender doesn’t have anyway.
import { content, fields, links, site } from '@ogygia/content';
export const guides = content({
loader: import.meta.og.loader.folder('../content/docs'),
schema: fields.page,
relations: (self) => ({ related: self }),
filter: (e) => !e.data.draft
});
export const docs = site({
outline: guides,
prevNext: 'graph',
checks: [links()],
data: {
title: 'Example',
description: 'Docs for Example.',
origin: 'https://example.com'
}
});Routing
The site serves every page from one catch-all route. Its +page.server.ts takes two functions straight off the site:
entrieslists every page’s address for the prerenderer, plus every old address a page declares inredirect_from, so the redirects are baked too.loadchecks the address before the page renders. A known page passes (after running the site’s checks on it). An old address answers a308to the page’s current address. Anything else answers a real404.
load reads params.slug and entries returns { slug } rows, so the parameter must be named slug ([...slug], or [slug] for a flat blog). They’re bound to the site, so you can export them by destructuring: export const { load, entries } = docs.
load is the part people forget. Without it, an unknown address reaches the page component with no view and fails there as a 500 instead of a 404, and old addresses don’t redirect.
load needs no configured mount prefix (/docs here): it takes the matched slug off the end of the URL’s path, which is what mountBase(url, slug) does if you need it yourself. The other parts of the site don’t work it out, so repeat the prefix as base in three places: remotes(docs, { base }), the shell’s base prop and every docs.search(q, { base }) call. Setting site({ base }) doesn’t reach the remotes or search (#2282); it’s the default only for nav, page, meta, switcher, check and the emissions.
import { docs } from '$lib/docs.server';
export const prerender = true;
export const entries = docs.entries;
export const load = docs.load;Rendering
A page component can’t import a server module, so it reads the site through remote functions. remotes(docs) makes them in a .remote.ts file: meta for the shell, page for one page, nav and search for custom chrome. They need Kit’s remote functions, Svelte’s async mode and ogygia’s transport hook; Publish a first collection shows the three settings. Remotes covers the options.
The page awaits its view and hands it to <Doc>, which lays out the title, the body, the on-this-page rail and the prev/next links. The body arrives as HTML that the server already rendered, and any island inside it wakes as usual.
The layout wraps every page in DocsShell: a header with search and a theme toggle, the sidebar, and a bottom bar on phones. It reads the shell’s data from the meta remote, so the layout never imports the corpus either. The two stylesheets are the stock look; Theming covers the shell’s options and other looks.
ogygia pages render with Kit’s client off, and the shell, <Doc> and the islands inside the body assume it. Set csr = false for the whole section in its +layout.ts (Move one route to islands explains what that changes):
import { remotes } from '@ogygia/content/server';
import { docs } from './docs.server';
export const { meta, page } = remotes(docs, { base: '/docs' });<script lang="ts">
import { page } from '$app/state';
import { Doc } from '@ogygia/content';
import * as docs from '$lib/docs.remote';
const view = (await docs.page(page.params.slug ?? ''))!;
</script>
<Doc {view} /><script lang="ts">
import DocsShell from '@ogygia/content/docs-shell';
import '@ogygia/content/theme.css';
import '@ogygia/content/shell.css';
import { meta } from '$lib/docs.remote';
let { children } = $props();
const shell = await meta();
</script>
<DocsShell meta={shell} base="/docs" title="Example">
{@render children()}
</DocsShell>export const csr = false;Page fields
The site reads a few frontmatter fields by name. Your schema has to keep them, or the site never sees them; fields.page from @ogygia/content keeps this set plus draft (Layer several schemas).
| Field | What the site does with it |
|---|---|
title | The page’s name in the sidebar, the pager, “keep reading” cards, search hits, llms.txt and the document <title>. With fields.page it’s required. With a schema of your own that leaves it out, the sidebar, pager, cards and llms.txt use the entry’s id, and the page itself, its <title> and search use the page’s address, which differs from the id under a group base or on a site with dimensions (#2286). |
summary | The line under the title, the <meta name="description">, the text on “keep reading” cards and in llms.txt. |
badge | A small label after the page’s name in the sidebar. |
redirect_from | Old addresses of the page, one string or a list. Each one redirects here; see Move a page without breaking links. |
related | Ids of pages to read next. It needs relations: (self) => ({ related: self }) on the collection, which turns the ids into entries; see Prev and next. |
draft isn’t read by the site. It hides a page only through the collection’s filter, as in the example above, and then it’s gone from the sidebar, the prerender list, search and its own address.
Site API
The site you get back (name it after what it is, docs or blog) is a set of functions. All of them run on the server and return plain data, so you can call them from a load, an endpoint, a remote or a test:
| Function | Returns |
|---|---|
docs.nav({ base?, slug? }) | The sidebar tree, with each href built for base. slug picks the version or language. |
docs.page(slug, { base? }) | One page’s view (below), or null for an unknown address. |
docs.meta({ base?, slug? }) | { nav, switcher, data } in one call: everything DocsShell needs. |
docs.switcher(slug, { base? }) | The version and language picker for a site with dimensions; null otherwise. |
docs.search(q, options?) | Ranked hits; see Search. |
docs.entries() | [{ slug }] for every page in reading order, then every old address. |
docs.load | The route guard from Mount it on a route. |
docs.check({ base? }) | Every check’s findings as data; see Checks. |
docs.emit.* | GET handlers for the sitemap, llms.txt, RSS, raw markdown and the search index; see Emissions. |
docs.outline | The outline itself, for resolving addresses by hand; see Read the outline yourself. |
docs.data, docs.components | The data and components you passed. |
nav, page, meta, switcher and check also take context; see Show drafts to some visitors.
Read one page’s view
docs.page(slug) returns a PageView. <Doc> renders all of it, and you can read any part to build your own layout:
| Field | What it holds |
|---|---|
slug | The page’s address within the site, such as regions/held. |
href | The address with base in front. |
entry | The collection’s entry: id, data, meta, body, rel, backlinks (and source when you call docs.page() yourself; the remote drops it). Render the body with <Region of={view.entry.body} />, importing Region from ogygia. |
section | The label of the top-level group the page sits in; empty for a page at the top level. |
crumbs | The groups from the top of the tree down to the page, as { label } objects. |
headings | The page’s headings, { depth, id, text }, for an on-this-page list: h2 to h4 by default. |
trail | prev and next (reading order), related (from the page’s related field) and suggested (the “keep reading” set prevNext picks). |
coordinate | On a site with dimensions: the version and language this address names. |
fallback | On a site with dimensions: { axis, from, to } when the page is shown from the default language, else null. |
Draw your own sidebar from the tree
docs.nav() returns a NavTree: a list of items, each one of three kinds told apart by kind. Groups hold more items, so the tree nests as deep as your folders. A custom sidebar snippet gets this same tree.
| Kind | Fields |
|---|---|
leaf | A page: slug, href (with base in front), title, and summary and badge when the page has them. |
group | A sidebar group: label, items, and badge and collapsed when the outline sets them. |
link | A plain link from the outline: label and href, as written. |
Drafts
The context option reads each request and returns a plain object, the read context. It’s passed to every collection’s filter as the second argument, so one site can show drafts in a preview and hide them from everyone else. The empty context {} is the public view. The remotes read it, and so does a prerender when your context returns {} for a plain request. Return {}, not { preview: false }, for the public case, or the site keeps a second copy of the public tree.
Keep the context small and made of a few known values, like { preview: true } or { plan: 'pro' }. The site builds its tree once per distinct context and keeps it, so a context that holds a user id would build and keep one tree per visitor.
When context throws, the site logs content_site_context_failed and answers that request with the public view.
Only load runs context for you. The other functions read the context you pass them (docs.page(slug, { context })), and the remotes remotes() makes pass none, so they always read the public view (#2282). A preview site therefore needs a page remote of its own that works the context out and passes it. It does what the built-in one does with the body: drops the server-only source reader and awaits the body so it crosses as HTML. Awaiting changes the body’s type, so the result is cast back to PageView, as remotes() does inside. The argument schema uses valibot here; any Standard Schema works. The route must not be prerendered for a preview to differ from the public page.
meta and nav read the public tree even here, so a draft page renders under preview but doesn’t appear in the sidebar.
import { content, fields, site } from '@ogygia/content';
export const guides = content({
loader: import.meta.og.loader.folder('../content/docs'),
schema: fields.page,
filter: (e, ctx) => !e.data.draft || ctx.preview === true
});
export const docs = site({
outline: guides,
context: ({ url }) =>
url.searchParams.has('preview') ? { preview: true } : {}
});import { getRequestEvent, query } from '$app/server';
import { remotes } from '@ogygia/content/server';
import type { PageView } from '@ogygia/content';
import * as v from 'valibot';
import { docs } from './docs.server';
export const { meta } = remotes(docs, { base: '/docs' });
export const page = query(v.string(), async (slug) => {
const { url } = getRequestEvent();
const context = url.searchParams.has('preview')
? { preview: true }
: {};
const view = await docs.page(slug, {
base: '/docs',
context
});
if (!view) return null;
const { source: _source, body, ...entry } = view.entry;
return {
...view,
entry: { ...entry, ...(body ? { body: await body } : {}) }
} as PageView;
});Configuration
site() takes one object. outline is the only required key.
| Option | Default | What it does |
|---|---|---|
outline | required | The arrangement: a collection, a list of outline items, an outline() value or a dimensions() value. |
data | none | { title, description, origin }, all three required. Emissions take their defaults from it. |
base | '' | The mount prefix nav, page, meta, switcher, check and the emissions use when a call passes no base. search and remotes() don’t read it; give them base too (#2282). |
prevNext | 'order' | What “keep reading” suggests: 'order' (the next page), 'graph' (the page’s related pages, else the next one) or false (nothing). See Prev and next. |
trail | 'site' | 'group' stops prev and next at the edge of each top-level section. |
checks | [] | Checks run on each page in load and over every page in check(), such as links(). See Checks. |
context | none | Reads a request and returns the read context. See Show drafts to some visitors. |
components | {} | Components that replace how markdown elements render. See Swap how markdown elements render. |
search | Orama | { engine } replaces the search engine for docs.search. See Use another engine. |
redirects | reads data.redirect_from | (entry) => … returns a page’s old addresses (a string, a list or nothing) from somewhere else. Only used when outline is a collection or a list; see redirects. |
Next steps
- Sidebar: the sidebar’s order and groups, by convention or by hand, redirects and prev/next.
- Versioning and i18n: one site across several versions or languages, with a switcher.
- Theming:
DocsShell,BlogShell,Docand every other component, the themes, and your own shell withFrame. - Remote functions: the site’s data as Kit remote functions, prerendered or per request.
- Search: a search box over a static index, a server query or a page without JavaScript.
- Sitemap and RSS:
sitemap.xml,llms.txt, RSS and each page as raw markdown. - Link checking: broken links and rules of your own, caught at build time.
Do and don’t
- Do keep
site()and its collections in a server module, and let pages read it through remotes. - Do export both
entriesandloadfrom the page route. Withoutload, unknown addresses answer 500 and old ones don’t redirect. - Do keep the fields the site reads in your schema (
fields.pagedoes), or they’re dropped before the site sees them. - Don’t pass a raw
entry.bodyover the wire yourself.docs.pagethrough a remote awaits it into HTML, which is the form that can cross. - Don’t put a user id in the read context. One tree is built and kept per distinct context.