cms-client
Populate React and TanStack Start pages with Median blocks using cms-renderer
cms-renderer is the Median client. You build pages from blocks in the CMS; your site maps each block type to a React component and renders whatever the page holds. This docs site is built exactly this way.
1. Install and configure
bun add cms-renderer
MEDIAN_API_KEY=sk_... # server only
DATASET_ENDPOINT=https://... # the Median page service
VITE_WEBSITE_ID=... # your website id
Create one client on the server. It holds the key, so never import it in client code:
// src/lib/median.server.ts
import { Median } from 'cms-renderer';
import { registry } from './block-registry';
export const median = new Median({
apiKey: process.env.MEDIAN_API_KEY,
datasetEndpoint: process.env.DATASET_ENDPOINT!,
websiteId: process.env.VITE_WEBSITE_ID!,
registry,
revalidate: 60, // seconds a published page stays cached
});
2. Generate types for your components
// scripts/generate-schemas.ts
const schemas = await median.schemas();
await writeFile('src/generated/cms-schemas.ts', await median.generateSchemas(schemas));
You get a Zod schema and a TypeScript type per component, e.g. Header. Re-run it when you change a component in the CMS.
3. Write a block
A block is a React component that receives its typed content:
// src/components/HeroBlock.tsx
import type { BlockComponentProps } from 'cms-renderer';
import type { Hero } from '@/generated/cms-schemas';
export default function HeroBlock({ content }: BlockComponentProps<Hero>) {
return (
<section className="py-24 text-center">
<h1 className="text-5xl font-semibold">{content.title}</h1>
<p className="mt-4 text-lg">{content.subtitle}</p>
</section>
);
}
Blocks also receive layout, routeParams, language and path.
4. Register it
The registry maps a block type (the UI element's name in the CMS) to its component. Unregistered blocks are skipped.
// src/lib/block-registry.ts
import type { BlockComponentRegistry } from 'cms-renderer';
import HeroBlock from '@/components/HeroBlock';
export const registry: Partial<BlockComponentRegistry> = {
hero: HeroBlock,
};
The docs site registers header, sidebar, content and footer this way.
5. Render every URL with one route
// src/routes/$.tsx
import { createFileRoute, notFound } from '@tanstack/react-router';
import { createServerFn } from '@tanstack/react-start';
import { ParametricPage } from 'cms-renderer';
import { registry } from '@/lib/block-registry';
const getPage = createServerFn({ method: 'GET' })
.validator((path: string) => path)
.handler(async ({ data }) => {
const { median } = await import('@/lib/median.server');
return median.resolveComponent(data);
});
export const Route = createFileRoute('/$')({
loader: async ({ params }) => {
const page = await getPage({ data: `/${params._splat ?? ''}` });
if (!page) throw notFound();
return page;
},
component: () => <ParametricPage page={Route.useLoaderData()} registry={registry} />,
});
resolveComponent makes one request for the page's blocks and the documents its URL is bound to. ParametricPage renders them and never sees the key.
6. Preview drafts inside the CMS
Add a preview route that reads the draft and adds the CMS edit overlay:
const page = await median.resolveComponent(path, { preview: true });
<ParametricPreview page={page} registry={registry} cmsUrl="https://app.mediancms.com" />
7. Build it statically (optional)
The docs site pre-renders every published URL at build time:
for (const path of await median.listPages()) {
pages[path] = await median.resolveComponent(path);
}
Pattern templates
A template like /{category}/{post} serves one page per document of its component. Edit its blocks once in the Template Builder and every document's page follows. Publish the template and a document's page goes live when the document is published. Only a page with its own blocks is stored, as an override.
Rich text renders with Markdown from cms-renderer/markdown; import cms-renderer/markdown.css for its styles.