Customization

Custom Pages and Actions

Add your own pages and one-click buttons to the admin.

A custom page adds a view of your own to the admin, like a dashboard, a report or a tool. An action adds a button that runs code on the server, like a deploy. This page shows how to add both.

Add a page

pages adds pages by key. This one lists the latest posts:

tenon.config.ts
import { mountStats } from "./src/admin/stats.client";

export default defineCms({
  // ...
  admin: {
    pages: {
      stats: {
        title: "Stats",
        nav: { icon: "chart-bar" },
        load: async ({ cms }) => ({
          posts: await cms.find("posts", { limit: 5, select: { title: true }, populate: false }),
        }),
        mount: mountStats,
      },
    },
  },
});
src/admin/stats.client.ts
import { renderVue } from "tenoncms/admin/vue";
import Stats from "./Stats.vue";

export const mountStats = renderVue(Stats);
src/admin/Stats.vue
<script setup lang="ts">
import type { PageContext } from "tenoncms/admin";

defineProps<PageContext<{ posts: { id: string; title: string }[] }>>();
</script>

<template>
  <ul>
    <li v-for="post in data.posts" :key="post.id">
      <a :href="`${tenon.base}/content/collections/posts/${post.id}`">{{ post.title }}</a>
    </li>
  </ul>
</template>

A page renders like a custom field: a mount function from a .client.ts module, written in plain DOM, Vue, React or another library. It gets this context:

KeyValue
paramsThe route params, like { id: "42" } for /reports/:id
queryThe query string, like { tab: "body" }
dataWhat the page's load returned. undefined without one.
tenonHelpers from the admin

Helpers

Every context in the admin has tenon, for pages and fields alike:

HelperWhat it does
baseThe admin path, like /admin. Prefix your links with it.
navigate(path)Opens an admin view without a reload, like /content/settings.
reload()Runs the load again, and passes the new data.
localeThe admin language, en or de.

A plain link under base also opens in the admin without a reload. To change content from a page, use createClient(). The editor's session makes writes work.

Routes

A page opens at /<key>, or at its route. Where it opens depends on nav.display:

nav.displayURL of route: "/stats"Layout
content, the default/admin/content/statsNext to the content menu
settings/admin/content/settings/statsNext to the content menu
main or none/admin/statsFull width
  • A route can have params, like route: "/reports/:id". Use nav: { display: "none" } for such a detail page.
  • A page on a built-in path replaces that view. route: "/collections/posts" replaces the list of posts.
  • The admin has no start page of its own: / opens the content. A page with route: "/" and nav: { display: "main" } becomes the start page, like a dashboard.

title, description and nav work as for collections. See Navigation.

Load data

load runs on the server when the page opens, and again when its params or query change. The page gets the result as data.

  • It gets the signed-in user, their cms, the admin locale, and the params and query of the route.
  • params and query come from the browser. Check them like any other input.
  • It MUST only read, and MUST return JSON.
  • Return only what the page needs. Being signed in does not make every server value fit for the browser.
  • Reads follow the editor's perspective, so load shows drafts in the Drafts view.

When only the params or query change, the admin calls update(ctx) instead of mounting the page again. In Vue and React, these are new props, so watch params to fetch again.

The page header

pageHeader sets the bar above the page:

ValueHeader
trueThe title. The default for content and settings pages.
falseNone. The default for main and none pages.
A stringThat text
[{ label, to }]Breadcrumbs. to is relative to the admin path.
A browser exportA function (crumbs, ctx) that returns breadcrumbs or an HTML string. It MUST be synchronous.
The admin does not escape the HTML string. Escape every value from the route, like params.

Actions

An action is a button in the top bar or the content menu. A click runs code on the server, and the admin shows the result as a toast. Actions need no browser code:

tenon.config.ts
admin: {
  actions: {
    deploy: {
      label: "Deploy",
      nav: { icon: "rocket" },
      confirm: "Deploy the site now?",
      run: async () => {
        const res = await fetch(process.env.DEPLOY_HOOK_URL!, { method: "POST" });
        if (!res.ok) throw new Error(`The deploy hook answered ${res.status}`);
        return "Deploy started";
      },
    },
  },
},
The text of the button.Runs on the server. It MAY return a text for the toast. A throw shows its message. cms belongs to the user who clicked, so their rules apply.An http or https URL instead of run. The entry opens it in a new tab.A question the admin asks first. Cancel runs nothing. Only with run.display is main (the top bar, the default) or content (the content menu). The other keys work as for navigation.Who sees and runs the action, like an access rule. Default: admins. API keys cannot run actions.
  • For a link, access only hides the entry. The URL itself stays public.
  • While an action runs, its button is disabled.