Pages
If you're building a website and need pages that can nest, you usually need the same few elements. Slug, seo, title and so on. pagesPreset() is more or less a block-based shortcut with sensible defaults. Each page stores its full path, so a read by URL is a plain filter.
Example
import { defineCms } from "tenoncms";
import { pagesPreset } from "tenoncms/presets";
export default defineCms({
// ...
presets: [
pagesPreset({
seo: true,
blocks: {
hero: {
label: "Hero",
fields: { title: { type: "text", required: true }, image: { type: "image" } },
},
text: { label: "Text", fields: { body: { type: "richtext" } } },
},
}),
],
});
Find a page by its URL:
const page = await cms.findOne("pages", { where: { path: "/about/team" } });
Render it
Render all pages with one catch-all route. Your other routes, like / or /posts/..., win over it.
---
// src/pages/[...path].astro
import { Blocks } from "tenoncms/astro/components";
import Hero from "../blocks/Hero.astro";
import Text from "../blocks/Text.astro";
export const prerender = false;
const path = `/${Astro.params.path ?? ""}`;
const page = await Astro.locals.tenon.findOne("pages", { where: { path } });
if (!page) return new Response(null, { status: 404 });
---
<main>
<h1>{page.title}</h1>
<Blocks blocks={page.content} components={{ hero: Hero, text: Text }} />
</main>
<!-- app/pages/[...path].vue -->
<script setup lang="ts">
import Hero from "~/components/blocks/Hero.vue";
import Text from "~/components/blocks/Text.vue";
const route = useRoute();
const { data: page } = await useTenon().findOne("pages", () => ({
where: { path: route.path },
}));
if (!page.value) throw createError({ statusCode: 404, statusMessage: "Page not found" });
</script>
<template>
<main v-if="page">
<h1>{{ page.title }}</h1>
<TenonBlocks :blocks="page.content" :components="{ hero: Hero, text: Text }" />
</main>
</template>
// src/routes/$.tsx
import { createFileRoute, notFound } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/react-start";
import { Blocks } from "tenoncms/react";
import cms from "../../tenon.config";
import { Hero } from "../blocks/Hero";
import { Text } from "../blocks/Text";
const getPage = createServerFn()
.validator((path: string) => path)
.handler(async ({ data: path }) => {
const page = await cms.findOne("pages", { where: { path } });
if (!page) throw notFound();
return page;
});
export const Route = createFileRoute("/$")({
loader: ({ params }) => getPage({ data: `/${params._splat ?? ""}` }),
component: Page,
});
function Page() {
const page = Route.useLoaderData();
return (
<main>
<h1>{page.title}</h1>
<Blocks blocks={page.content} components={{ hero: Hero, text: Text }} />
</main>
);
}
See Pages and SEO for head tags, locales and live preview.
Fields
The collection gets these fields:
| Field | What it is |
|---|---|
title | The page title. Required. |
slug | The last part of the URL, filled from the title. Required. |
parent | The page above this one. Editors pick it in front of the slug input. |
path | The full URL, like /about/team. Hooks set it. Editors do not see it. |
content | With blocks: the sections of the page. |
seo | With seo: true: SEO title, description, share image and "hide from search engines". |
The collection also has permalink: "{path}", so the sitemap and links know each page's URL, and schedule: true.
How paths work
- A top-level page with the slug
indexhas the path/. Any other top-level page has/<slug>. - A child page has the path of its parent plus its slug.
- When a page moves or its slug changes, the paths of all pages below it change too.
- Two pages cannot have the same path. A page with children cannot be deleted.
| Page | Parent | Path |
|---|---|---|
index | / | |
about | /about | |
team | about | /about/team |
team | jobs | /jobs/team |
Sections
blocks adds a content field of type blocks. Pass the blocks inline, or the names of shared blocks from defineCms({ blocks }):
defineCms({
blocks: { hero, text },
presets: [pagesPreset({ blocks: ["hero", "text"] })],
});
Without blocks, there is no content field. Add your own with fields:
pagesPreset({
fields: { body: { type: "richtext" } },
}),
SEO
seo: true adds the seo group and splits the form into the tabs Content and SEO. The group has an image field, so it needs media.
Populate the share image when you read a page:
const page = await cms.findOne("pages", {
where: { path },
populate: { seo: { image: true } },
});
page.seo?.title;
page.seo?.image;
seo() is the same group as a part. Spread it into any collection:
import { defineCollection } from "tenoncms";
import { seo } from "tenoncms/presets";
export const posts = defineCollection({
fields: {
title: { type: "text", required: true },
...seo({ title: "{title} · Blog" }).fields,
},
});
seo() takes title and description templates for documents without their own. They win over the site's seo global. Placeholders like {title} fill in from the document. See Pages and SEO.
Options
"pages".content field of type blocks with these blocks. Without it, there is no content.seo group and splits the form into the tabs Content and SEO. Needs media. Default false.All other options of a collection pass through, like label, localized, access or tabs.
Build your own
page() returns the fields, hooks and permalink of pages with nested URLs. Use it when you want your own collection with the page logic inside:
import { defineCollection } from "tenoncms";
import { page, seo } from "tenoncms/presets";
const pageParts = page();
export const pages = defineCollection({
label: "Pages",
titleField: "title",
permalink: pageParts.permalink,
fields: {
...pageParts.fields,
...seo().fields,
body: { type: "richtext" },
},
hooks: pageParts.hooks,
});
page({ collection: "docs" }) points parent at another collection, for pages that live in docs.
Hooks
The hooks of page() have names, so you can remove one by name:
| Stage | Name | What it does |
|---|---|---|
beforeChange | validParent | Rejects a parent that is the page itself or a page below it |
beforeChange | buildPath | Sets path from the parent's path and the slug |
beforeChange | uniquePath | Rejects a path that another page already has |
afterChange | updateChildPaths | Updates the paths of all pages below when path changes |
const docs = page({ collection: "docs" });
// Allow two pages with the same path.
const { uniquePath, ...beforeChange } = docs.hooks.beforeChange;
export const docsCollection = defineCollection({
fields: docs.fields,
hooks: { beforeChange, afterChange: docs.hooks.afterChange },
});
With pagesPreset(), replace a hook by its name in hooks:
pagesPreset({
hooks: {
beforeChange: {
uniquePath: () => {},
},
},
}),
Nested paths with other field names
nestedPaths() gives the same hooks for fields with other names, like categories in a shop:
import { defineCollection } from "tenoncms";
import { nestedPaths } from "tenoncms/presets";
export const categories = defineCollection({
titleField: "name",
fields: {
name: { type: "text", required: true },
handle: { type: "slug", from: "name", parent: "parentCategory", path: "url" },
parentCategory: { type: "relation", collection: "categories", onDelete: "restrict" },
url: { type: "text", readOnly: true },
},
hooks: nestedPaths({ slug: "handle", parent: "parentCategory", path: "url" }),
});
The slug's path option MUST name the same field as nestedPaths(), so the admin shows the right prefix.