Customization

Plugins

Add features from one import.

A plugin bundles a feature into one import: collections and globals, hooks, API routes and admin pieces. You add it once in tenon.config.ts, with nothing else to wire up. This page shows how to use a plugin and the plugins that come with Tenon.

Use a plugin

List plugins in plugins:

tenon.config.ts
import { defineCms } from "tenoncms";
import { RedirectsPlugin } from "tenoncms/plugins/redirects";

export default defineCms({
  // ...
  plugins: [RedirectsPlugin()],
});

A plugin can add:

A plugin's additions come before yours. Its hooks run before your root hooks, and its lifecycle hooks before yours. A collection, global, field or page name that you and a plugin both use stops the server at startup.

Bundled plugins

PluginWhat it does
tenoncms/plugins/redirectsRedirects from old paths, created when a page moves
tenoncms/plugins/exampleA starting point to copy. It uses every plugin option once.

Redirects

RedirectsPlugin() adds a Redirects collection to the admin. When the path of a page changes, it creates a redirect from the old path:

tenon.config.ts
plugins: [RedirectsPlugin({ collections: ["pages"], field: "path", locales: ["en", "de"] })],
The collections whose path changes create a redirect. Default ["pages"].The path field in those collections. Default "path".The languages to pick for a redirect. Without it, the language is a text input. An empty language applies to all.
  • A redirect has from, to, permanent (301, the default, or 302) and locale.
  • Redirects that pointed to the old path follow the move, so there is never more than one hop.
  • Redirect Tools in the admin tests a path and imports many redirects at once.

Resolve redirects on your site

The plugin stores redirects. Your site follows them in a middleware, before any page renders. resolveRedirect() returns the target and the status, or undefined:

// src/middleware.ts
import { defineMiddleware } from "astro:middleware";
import { resolveRedirect } from "tenoncms/plugins/redirects";
import cms from "../tenon.config";

export const onRequest = defineMiddleware(async (context, next) => {
  const redirect = await resolveRedirect(cms, context.url.pathname);
  return redirect ? context.redirect(redirect.to, redirect.status) : next();
});
  • With languages, pass the page's language as the third argument: resolveRedirect(cms, path, "de"). A redirect for that language wins over one for all.
  • A site on another origin asks GET /_tenon/redirects/lookup?path=/old-page. It answers { to, status }, or 404. Add &locale=de for a language.

The example plugin

ExamplePlugin() from tenoncms/plugins/example is not a feature. It is code to copy when you write your own plugin. It adds a quotes collection with a custom field, a settings global, a scheduled job, API routes, admin pages, admin bar items and a rich text extension.

Its browser code exists in plain DOM, Vue and React. renderer: "html" | "vue" | "react" picks one. Its source is in the Tenon repository, under packages/plugins/src/example/.

What a plugin can define

A plugin's additions merge before yours, in the order of plugins. A collection, global, task or field name that you and a plugin both use stops the server at startup.

Unique, with lowercase letters, digits and -. It prefixes the plugin's routes and task names. Names of core routes are reserved: account, admin, admin-bar, admin-brand, api-keys, auth, auth-status, collections, export, globals, import, invites, jobs, localization, media, preview, schema and setup.Collections from defineCollection(). They are typed for the site, so cms.find("announcements") works.Globals from defineGlobal(). A global with nav: { display: "settings" } makes a settings page for the plugin.Hooks for every collection and global. They run after the collection's own hooks and before your root hooks.Hooks of the media library. They run after the hooks in media.hooks.Routes at /_tenon/<name> that need a session, for the plugin's admin pages. They run after the CSRF and session checks. c.var.user is the signed-in user, and c.var.cms runs with that user. API keys get 403.Routes at /_tenon/<name> without a session, for the site. They run before the CSRF and session checks, so a public route that writes MUST protect itself.Hono middleware for every request under /_tenon, sign-in included, before any route. c.var.cms has no user. Return a response to stop the request, for example for a rate limit.Hono middleware for requests with a session: content writes, /schema and the routes of every plugin. Writes without a session skip it, API key writes included.Jobs of the plugin. A task name MUST start with <name>:, like announcements:expire. scheduledTasks MAY name only the plugin's own tasks.Rich text extensions, like your own richtext.Fields, pages and actions and admin bar items, like your own admin. A plugin never sets the bar's position and never turns the bar off.Lifecycle hooks. They run before yours.

Routes and middleware map errors like core does: a ValidationError answers 400 with the field errors, a ConflictError answers 409, anything else 500.

Write your own

A plugin is an object from definePlugin(). Use one to share a feature between projects, or to keep a feature of your own site in one place. See Write a plugin.