Plugins
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:
import { defineCms } from "tenoncms";
import { RedirectsPlugin } from "tenoncms/plugins/redirects";
export default defineCms({
// ...
plugins: [RedirectsPlugin()],
});
A plugin can add:
- collections and globals. They are typed like yours, so
cms.find("redirects")works. - hooks for every collection, and lifecycle hooks like
seed. - API routes under
/_tenon/<plugin name>, and middleware for the API. - jobs and their schedules.
- rich text extensions.
- fields, pages and actions, and admin bar items.
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
| Plugin | What it does |
|---|---|
tenoncms/plugins/redirects | Redirects from old paths, created when a page moves |
tenoncms/plugins/example | A 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:
plugins: [RedirectsPlugin({ collections: ["pages"], field: "path", locales: ["en", "de"] })],
["pages"]."path".- A redirect has
from,to,permanent(301, the default, or302) andlocale. - 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();
});
// server/middleware/redirects.ts
import cms from "#tenon/config";
import { resolveRedirect } from "tenoncms/plugins/redirects";
export default defineEventHandler(async (event) => {
const redirect = await resolveRedirect(cms, getRequestURL(event).pathname);
if (redirect) return sendRedirect(event, redirect.to, redirect.status);
});
// src/start.ts
import { createMiddleware, createStart } from "@tanstack/react-start";
import { resolveRedirect } from "tenoncms/plugins/redirects";
import cms from "../tenon.config";
const redirects = createMiddleware().server(async ({ request, next }) => {
const redirect = await resolveRedirect(cms, new URL(request.url).pathname);
if (redirect) return Response.redirect(new URL(redirect.to, request.url), redirect.status);
return next();
});
export const startInstance = createStart(() => ({ requestMiddleware: [redirects] }));
- 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 }, or404. Add&locale=defor 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.
-. 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.defineCollection(). They are typed for the site, so cms.find("announcements") works.defineGlobal(). A global with nav: { display: "settings" } makes a settings page for the plugin.media.hooks./_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./_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./_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./schema and the routes of every plugin. Writes without a session skip it, API key writes included.<name>:, like announcements:expire. scheduledTasks MAY name only the plugin's own tasks.richtext.items, like your own admin. A plugin never sets the bar's position and never turns the bar off.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.
Related resources
- Write a plugin:
definePlugin(), and how to publish a plugin. - Custom fields: the admin pieces a plugin adds.
clientRef(): how a plugin points to its browser code.- Hooks: when plugin hooks run.