How Tenon works
This page explains how Tenon runs inside your app. Read it when you want to know what the integration does for you, or when you set up a framework without one.
Tenon is one object in your app: the CMS instance that tenon.config.ts exports. Your server code, the HTTP API and the admin all talk to this instance. There is no second service, and Tenon has no server or build step of its own.
Local API and HTTP API
The local API is the set of methods on the instance, like cms.find() and cms.create(). Your server code calls them directly, with no HTTP and full types.
The HTTP API is cms.fetch(request). It takes a web Request and returns a Response, so any server that speaks web standards can run it. The admin and the browser client use it.
Both ways end in the same local API. Validation, access rules, hooks and transactions live there, so a write behaves the same from your code, the HTTP API and the admin. The HTTP API is only a thin layer that reads the request and the session.
There is one difference: who is asking.
- The instance you import skips the access rules. Your server code is trusted.
- The HTTP API applies them, for the user of the request. To do the same in server code, use
cms.withSession(request). See Access control.
What happens at startup
Nothing connects when your app imports tenon.config.ts. defineCms() only checks the config. If something is missing or invalid, it throws one error that lists every problem, so a bad deploy fails at startup instead of on the first request.
The first query to the database runs these steps once:
- Connect to the database and run the migrations. Tenon creates and updates its own tables.
- Run
seed, once per database. - Run
onInit.
Every query waits until these steps are done. If one fails, the next query tries again. Serverless hosts start often, so keep onInit fast. See Lifecycle hooks.
How Vite fits in
Your app's Vite build does the work Tenon needs. The Astro integration and the Nuxt module do it for you. Other Vite frameworks, like TanStack Start, add tenonAdmin() from tenoncms/vite and a few route files.
The server side
tenon.config.ts is a normal module of your app. Nothing loads it in a special way: a server route imports it, and Vite compiles it into the server build like any other file. In development, a change to it reloads without a restart.
A server route only passes the request to cms.fetch():
- Astro and Nuxt add these server routes for you. Their route files import your config through the alias
#tenon/config, which points totenon.config.ts. - TanStack Start and other Vite frameworks have no integration that adds routes. You write each route file yourself, and it imports the config directly:
import { createFileRoute } from "@tanstack/react-router";
import cms from "../../tenon.config";
const handle = ({ request }: { request: Request }) => cms.fetch(request);
export const Route = createFileRoute("/_tenon/$")({
server: { handlers: { ANY: handle } },
});
The admin
The admin needs more than a route. It is browser code, and so are your custom fields, admin pages and admin bar buttons. That is the part a Vite plugin does. Astro and Nuxt include it. On other Vite frameworks, add tenonAdmin() to vite.config.ts.
The plugin reads tenon.config.ts as source text. It never runs it, because the config holds server code like the database and the secret. From the source it takes:
admin.path: the path the admin page is built for.- Every
clientRef(): the browser code you point to, likeclientRef("./src/admin/color.ts#mountColor"). The plugin bundles it with hot reload. That is why aclientRef()MUST be a literal string.
It then builds the admin as a second entry of your app's client build. The admin ships with your site, so there is nothing extra to deploy. On TanStack Start, a route file serves the page from #tenon/admin-html. Its path MUST match admin.path.
With Nitro, tenonAdmin() also runs the due jobs every minute. See The clock.
The CLI
The tenon CLI loads tenon.config.ts through Vite too. So astro:env and Nuxt's runtime config work there as well. See the CLI.
Comparison of the setups
| Setup | Server routes | Admin |
|---|---|---|
| Astro | Added by the integration | Added by the integration |
| Nuxt | Added by the module | Added by the module |
| TanStack Start, other Vite | Route files you write | tenonAdmin() and a route file you write |
| No Vite | Routes you write | You mount it yourself |
Astro, Nuxt and tenonAdmin() with Nitro also run the due jobs every minute. Otherwise, you run them yourself. See The clock.
Related resources
- Configuration: the config file, environment variables and the routes Tenon adds.
- Add to an existing site: the setup for each framework.
- Reading content: the local API.
- HTTP API and client: the HTTP API.
- Headless: Tenon without a site, or with a frontend on another origin.
- Other frameworks: running Tenon without an integration.