Presets

Pages

Pages with nested URLs like /about/team, with optional sections and SEO.

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

tenon.config.ts
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>

See Pages and SEO for head tags, locales and live preview.

Fields

The collection gets these fields:

FieldWhat it is
titleThe page title. Required.
slugThe last part of the URL, filled from the title. Required.
parentThe page above this one. Editors pick it in front of the slug input.
pathThe full URL, like /about/team. Hooks set it. Editors do not see it.
contentWith blocks: the sections of the page.
seoWith 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 index has 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.
PageParentPath
index/
about/about
teamabout/about/team
teamjobs/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:

cms/collections/posts.ts
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

The collection name. Default "pages".Adds a content field of type blocks with these blocks. Without it, there is no content.Adds the seo group and splits the form into the tabs Content and SEO. Needs media. Default false.More fields, on the Content tab. A field with the key of a preset field replaces it.More hooks. They run after the preset's hooks. A hook with a preset hook's name replaces it.

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:

cms/collections/pages.ts
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:

StageNameWhat it does
beforeChangevalidParentRejects a parent that is the page itself or a page below it
beforeChangebuildPathSets path from the parent's path and the slug
beforeChangeuniquePathRejects a path that another page already has
afterChangeupdateChildPathsUpdates 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:

cms/collections/categories.ts
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.