Getting started
Quick start
A brick, a collection, a page that renders it, and an admin to edit it.
This walk-through builds the smallest useful Mortar site: one brick, a pages collection, a route that renders a page, and the admin.
1. Define a brick
A brick is a component plus the schema of its data. The schema is built with fields.* helpers, and the component receives the data, already validated and with defaults filled in, as props.data.
// src/bricks/hero.tsx
import { defineBrick, fields } from '@mortar-kit/core';
export const hero = defineBrick({
name: 'hero',
label: 'Hero',
schema: {
heading: fields.text({ label: 'Heading', required: true, default: 'Hello' }),
intro: fields.textarea({ label: 'Intro' }),
},
component: (props) => (
<section>
<h1>{props.data.heading}</h1>
<p>{props.data.intro}</p>
</section>
),
});
export const bricks = [hero];
2. Declare a collection
Pages aren't built in: a site declares its own pages collection. With body: true, each page also has an ordered list of bricks.
// src/collections.ts
import { defineCollection, fields } from '@mortar-kit/core';
export const pages = defineCollection({
name: 'pages',
label: 'Pages',
fields: {
title: fields.text({ label: 'Title', required: true }),
},
body: true,
url: '/:slug',
home: 'home',
reservedSlugs: ['admin'],
});
export const collections = [pages];
3. Store content
createContent applies Mortar's rules over a store. On the server, use the JSON-file store, and let createContentServer turn it into functions the browser can call. Reads are open to everyone; writes need a signed-in editor once you add sign-in (auth: false leaves them open while you try things out locally):
// src/server/mortar.ts
import { createContent } from '@mortar-kit/core';
import { createContentServer, createJsonFileStore } from '@mortar-kit/core/server';
import { bricks } from '../bricks/hero.tsx';
import { collections } from '../collections.ts';
export const content = createContent({ store: createJsonFileStore({ dir: 'content' }), collections, bricks });
export const server = createContentServer({ content, auth: false });
// src/server/content.ts
'use server';
import { server } from './mortar.ts';
export const list = server.list;
export const get = server.get;
export const save = server.save;
export const remove = server.remove;
export const move = server.move;
The other functions it makes (listAssets, uploadAsset and the rest) are for assets; see Storage for the full list. A page is now a file such as content/pages/home.json.
Then build the browser side with contentClient: cached reads for your pages, fresh reads and writes for the admin:
// src/content.ts
import { contentClient } from '@mortar-kit/core';
import { query } from '@solidjs/router';
import { GET } from '@solidjs/web/server-functions';
import * as server from './server/content.ts';
export const { source, freshSource, content: adminContent } = contentClient(server, { read: GET, cache: query, onWrite: () => query.clear() });
4. Render a page
Provide the reads to your app with ContentContext:
// src/App.tsx
import { ContentContext } from '@mortar-kit/core';
import { source } from './content.ts';
export default function App() {
return <ContentContext value={source}>{/* your router */}</ContentContext>;
}
Then read a document with useContent() and render its body with <PageView>:
// src/routes/page.tsx
import { PageView, useContent } from '@mortar-kit/core';
import { createMemo, Show } from 'solid-js';
import { bricks } from '../bricks/hero.tsx';
import { pages } from '../collections.ts';
export function PageRoute(props: { slug: string }) {
const content = useContent();
const page = createMemo(() => content.get(pages, { slug: props.slug }));
return (
<Show when={page()}>
{(p) => <PageView bricks={bricks} entries={p().entries ?? []} />}
</Show>
);
}
5. Mount the admin
The admin is a route in your app. It needs reads and writes, and the URL of a preview route that renders <PreviewPage>:
// src/routes/admin.tsx
import { Admin } from '@mortar-kit/admin';
export default function AdminRoute() {
return <Admin bricks={bricks} collections={collections} content={adminContent} previewUrl="/admin/preview" />;
}
// src/routes/admin-preview.tsx
import { PreviewPage } from '@mortar-kit/admin/preview';
export default function AdminPreview() {
return <PreviewPage bricks={bricks} content={freshSource} />;
}
adminContent and freshSource come from contentClient: the admin reads fresh, so it always shows what is saved, while your pages keep their cached reads. Load both routes lazily, so admin code stays out of your public pages' bundle.