@ogygia/content
Blocks
A page a visual editor or a CMS puts together is data, a tree of blocks that each name a component and its props. blocks() renders that tree through a registry you own, and a page loads the code and styles of only the blocks it uses.
On this page
Some pages aren’t written by developers. A marketing team lays out a landing page in a visual editor, or a CMS decides which components appear and in what order, and your app renders what it’s given. That page is data: a tree of blocks, where each block names a component type, its props and its children.
blocks() renders that tree. It’s a collection source, like markdown(): each entry’s body is the whole tree, rendered, and you place it with <Region of={entry.body} />. You decide which component each type means in a registry, and the CMS owns the arrangement. Each block imported with a mark is its own held region, so a page loads the styles of only the block types it uses, and the JavaScript of only the interactive ones.
The panel below is a block tree (a Hero, a Grid of two Features, and an interactive counter) rendered from data with this page’s registry.
Block registry
The registry is a plain object from a type name to a component. Write it in a .ts module, where a mark on an import makes the component a value that can travel as a region. How you import each block decides what it costs a page:
| Import | The block |
|---|---|
with { region: 'raw' } | Renders as server HTML and ships no JavaScript. Its stylesheet loads only on pages that use it. |
with { wake: 'load' } (or another schedule) | Renders as server HTML, then hydrates on that schedule. Its code and styles load only on pages that use it. |
| no mark | Renders as server HTML, but its styles are bundled with the page that imports the registry, used or not. |
Use region: 'raw' for static blocks and wake for interactive ones. A registry of a hundred plain imports costs every page the styles of all hundred.
A node whose type isn’t in the registry is never rendered as something else. It’s left out with its children, and dev prints content_block_unregistered. A static block that nests islands of its own still wakes them.
One line for a whole folder
One import per block drifts out of step with the folder: add a file, forget the line. import.meta.og.regions('./blocks/*.svelte') imports every match with region: 'raw' and keys each by its file name. Spread interactive blocks over it.
import Hero from './blocks/Hero.svelte' with { region: 'raw' };
import Grid from './blocks/Grid.svelte' with { region: 'raw' };
import Feature from './blocks/Feature.svelte' with {
region: 'raw'
};
import Counter from './blocks/Counter.svelte' with {
wake: 'load'
};
export const registry = { Hero, Grid, Feature, Counter };import Counter from './blocks/Counter.svelte' with {
wake: 'load'
};
export const registry = {
...import.meta.og.regions('./blocks/*.svelte'),
Counter
};JSON pages
A node is { type, props?, children? }: type names a registry entry, props go to its component, and children render inside it, as its children snippet. Nesting is what makes it a page tree rather than a flat list: a Grid holds Features, a section holds a grid. A node may carry an id too; the renderer ignores it.
A page file can be:
- an array of nodes;
- a single node;
{ blocks: [...], meta: {...} }, wheremeta(ordata) becomes the entry’sdata, checked by the collection’s schema. A page in the other two shapes has{}as its data.
A block component renders its children like any Svelte component: {@render children?.()}.
{
"meta": { "title": "Welcome" },
"blocks": [
{ "type": "Hero", "props": { "title": "Welcome" } },
{
"type": "Grid",
"children": [
{ "type": "Feature", "props": { "title": "Fast" } },
{ "type": "Feature", "props": { "title": "Typed" } }
]
},
{ "type": "Counter", "props": { "start": 3 } }
]
}Rendering
Point a collection at the page files with blocks(pages, registry, options?). pages is an import.meta.glob map or a raw source of your own (refs() and get() returning { id, value } records), as in Load pages from a headless CMS; ids come from the file names, as for any file source. options takes id to choose ids yourself and schedule (see below).
Then read an entry with get() and render its body with <Region>, as you would a markdown body. On a csr = false page, the simplest place is a universal +page.ts load (or the page component itself): both run only on the server there, so the body reaches the render as it is and renders in the same pass.
A body that has to cross the wire, returned from a remote function or from a server load whose data Kit serializes, must be awaited first (await entry.body), or it fails with held_region_plain_import.
A blocks collection holds JSON and a registry, not a compiled corpus. Defining it in an ordinary module, so universal code can import it, prints content_collection_not_server_only; that’s expected here.
Each block’s styles travel with it
The page never imports Hero or Feature, so their styles are in none of the page’s stylesheets. During the server render, each block that renders links its own stylesheet into <head>, once per type. A registry of a hundred blocks costs a page exactly the stylesheets of the blocks it shows.
import { content, blocks } from '@ogygia/content';
import { registry } from './blocks';
export const pages = content({
loader: blocks(
import.meta.glob('../content/pages/*.json'),
registry
)
});import { error } from '@sveltejs/kit';
import { pages } from '$lib/pages';
export const csr = false;
export const load = async ({ params }) => {
const page = await pages.get(params.slug);
if (!page) error(404, 'Not found');
return { page };
};<script lang="ts">
import { Region } from 'ogygia';
let { data } = $props();
</script>
{#if data.page.body}<Region of={data.page.body} />{/if}Per-block schedules
A wake mark gives every block of that type one schedule. When the data should decide (an editor ticks “interactive”), import the block with { region: 'raw' } and pass schedule. It gets each node’s props and returns { wake, margin? }. A wake it returns replaces the import’s mark; undefined keeps the mark, so only a region: 'raw' block stays HTML only. It applies to every node in the tree.
import { content, blocks } from '@ogygia/content';
import { registry } from './blocks';
export const pages = content({
loader: blocks(
import.meta.glob('../content/pages/*.json'),
registry,
{
schedule: (props) => ({
wake: props.interactive ? 'visible' : undefined
})
}
)
});Trees without collections
When you already hold a tree (a prop, a literal, one CMS call), you don’t need a collection. blocks.resolve(tree, registry, schedule?) turns it into nodes of { of, children? }, where of is a region: the same walk the blocks() source runs, unknown types skipped. Render the nodes with a small component you own. ogygia ships none; the component below is the whole recipe.
blocks.resolve signs each region: 'raw' block with your server’s key, so call it where that key is: in a load, a remote function, or the server render of a csr = false page. In the browser it throws raw_region_on_client. The page below sets csr = false, so its component, and BlockTree in it, render only on the server.
<script lang="ts">
import { Region } from 'ogygia';
import {
blocks,
type BlockNode,
type BlockRegistry,
type ResolvedBlockNode
} from '@ogygia/content';
import Self from './BlockTree.svelte';
let {
tree,
registry,
nodes
}: {
tree?: BlockNode[];
registry?: BlockRegistry;
nodes?: ResolvedBlockNode[];
} = $props();
const resolved = $derived(
nodes ?? blocks.resolve(tree, registry ?? {})
);
</script>
{#each resolved as node, i (i)}
<Region of={node.of}>
{#if node.children?.length}<Self
nodes={node.children}
/>{/if}
</Region>
{/each}import type { BlockNode } from '@ogygia/content';
export const csr = false;
export const load = () => {
const tree: BlockNode[] = [
{ type: 'Hero', props: { title: 'Welcome' } },
{ type: 'Counter', props: { start: 3 } }
];
return { tree };
};<script lang="ts">
import BlockTree from '$lib/BlockTree.svelte';
import { registry } from '$lib/blocks';
let { data } = $props();
</script>
<BlockTree tree={data.tree} {registry} />Headless CMS
A headless CMS serves pages as JSON over HTTP. Write a raw source over its API, reshape each result into a block tree with mapRaw, and hand it to blocks(). refs() lists every page for navigation and prerendering; get(id) fetches one page when it’s read. Everything else in your app stays as it is, because it’s a collection like any other.
The live demo uses local JSON in the same shape: /demo/builder/home and /demo/builder/pricing are whole pages assembled from data, where only the interactive block ships JavaScript.
import {
content,
blocks,
mapRaw,
type BlockNode,
type RawSource
} from '@ogygia/content';
import { registry } from './blocks';
type CmsElement = {
component?: {
name: string;
options?: Record<string, unknown>;
};
children?: CmsElement[];
};
type CmsPage = {
id: string;
data: { url?: string; title?: string; blocks?: CmsElement[] };
};
const API = 'https://cms.example.com/api/pages';
const toTree = (els: CmsElement[] = []): BlockNode[] =>
els
.filter((e) => e.component?.name)
.map((e) => ({
type: e.component!.name,
props: e.component!.options ?? {},
children: e.children ? toTree(e.children) : undefined
}));
const cms: RawSource<CmsPage> = {
async refs() {
const pages: CmsPage[] = await fetch(API).then((r) =>
r.json()
);
return pages.map((p) => ({
id: (p.data.url ?? p.id).replace(/^\//, ''),
value: p
}));
},
async get(id) {
const res = await fetch(
`${API}?url=/${encodeURIComponent(id)}`
);
const [page]: CmsPage[] = res.ok ? await res.json() : [];
return page ? { id, value: page } : null;
}
};
export const pages = content({
loader: blocks(
mapRaw(cms, (p) => ({
meta: { title: p.data.title ?? '' },
blocks: toTree(p.data.blocks)
})),
registry
)
});Code splitting
The registry can hold a thousand blocks while a page that names three loads three. Nothing special makes that happen. The registry is fixed when you build, the page data picks from it when it renders, and each block is a held region whose code and styles load only when it’s on the page. However large the registry grows, a page’s bundle stays the size of what it shows. Held regions covers how a region loads.
Do and don’t
- Do import static blocks
with { region: 'raw' }and interactive ones with awakemark. - Do keep the registry’s keys equal to the
typenames your CMS sends. - Do
await entry.bodywhen a blocks body is returned from a remote function. - Don’t call
blocks.resolvein the browser; it needs the server’s signing key. - Don’t import every block plainly in a large registry; each page would carry every block’s styles.