Custom Pages and Actions
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:
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,
},
},
},
});
import { renderVue } from "tenoncms/admin/vue";
import Stats from "./Stats.vue";
export const mountStats = renderVue(Stats);
<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:
| Key | Value |
|---|---|
params | The route params, like { id: "42" } for /reports/:id |
query | The query string, like { tab: "body" } |
data | What the page's load returned. undefined without one. |
tenon | Helpers from the admin |
Helpers
Every context in the admin has tenon, for pages and fields alike:
| Helper | What it does |
|---|---|
base | The 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. |
locale | The 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.display | URL of route: "/stats" | Layout |
|---|---|---|
content, the default | /admin/content/stats | Next to the content menu |
settings | /admin/content/settings/stats | Next to the content menu |
main or none | /admin/stats | Full width |
- A route can have params, like
route: "/reports/:id". Usenav: { 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 withroute: "/"andnav: { 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, theircms, the adminlocale, and theparamsandqueryof the route. paramsandquerycome 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
loadshows 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:
| Value | Header |
|---|---|
true | The title. The default for content and settings pages. |
false | None. The default for main and none pages. |
| A string | That text |
[{ label, to }] | Breadcrumbs. to is relative to the admin path. |
| A browser export | A function (crumbs, ctx) that returns breadcrumbs or an HTML string. It MUST be synchronous. |
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:
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";
},
},
},
},
cms belongs to the user who clicked, so their rules apply.http or https URL instead of run. The entry opens it in a new tab.run.display is main (the top bar, the default) or content (the content menu). The other keys work as for navigation.- For a link,
accessonly hides the entry. The URL itself stays public. - While an action runs, its button is disabled.
Related resources
- Custom fields: the
mountfunction in plain DOM, Vue and React. - Browser modules:
.client.tsmodules andclientRef(). - Admin: navigation, the admin bar and languages.
- HTTP API and client: read and write content from a page.