Frameworks

Astro

Set up Tenon in an Astro project.

The tenoncms/astro integration adds the API, the admin and the live preview to your Astro site. Pages read content through Astro.locals.tenon, typed from your config.

Create a project

Run the wizard and pick Astro:

npm create tenoncms@latest

The starter has astro.config.ts with the integration and the env schema, tenon.config.ts, a posts collection and two pages. Start it with npm run dev, open http://localhost:4321/admin and create the first admin. See create-tenoncms for every question and flag.

Add to an existing project

Tenon runs on the server, so your site needs a server adapter. This setup uses @astrojs/node and a SQLite file. For the other databases, see Databases.

Install the packages

npm install tenoncms @libsql/client @astrojs/node

Add the integration

Add the adapter and tenonCms(), and declare the variables in the env schema:

astro.config.ts
import node from "@astrojs/node";
import { defineConfig, envField } from "astro/config";
import tenonCms from "tenoncms/astro";

export default defineConfig({
  output: "server",
  adapter: node({ mode: "standalone" }),
  integrations: [tenonCms()],
  env: {
    schema: {
      TENON_SECRET: envField.string({ context: "server", access: "secret", min: 32 }),
      DATABASE_URL: envField.string({
        context: "server",
        access: "secret",
        default: "file:./cms.db",
      }),
      TENON_SITE_URL: envField.string({
        context: "server",
        access: "secret",
        url: true,
        optional: true,
      }),
    },
  },
});

output: "server" renders every page per request. Leave it out to keep prerendering, and set export const prerender = false on the pages that read live content.

Create the CMS instance

Create tenon.config.ts at the project root. It reads the variables from astro:env/server:

tenon.config.ts
import { DATABASE_URL, TENON_SECRET, TENON_SITE_URL } from "astro:env/server";
import { defineCms } from "tenoncms";
import { libsql } from "tenoncms/libsql";
import { posts } from "./cms/collections/posts";

export default defineCms({
  collections: { posts },
  database: libsql(DATABASE_URL),
  secret: TENON_SECRET,
  site: { url: TENON_SITE_URL, name: "My Site" },
});

For the posts collection, see Add a collection.

Set the secret

Generate a secret with openssl rand -hex 32 and put it in .env:

.env
TENON_SECRET=<your secret>

Keep .env, cms.db* and .uploads out of git.

Create the admin account

Run npm run dev and open http://localhost:4321/admin. The first account you create is the admin.

Read content

Astro.locals.tenon is the CMS instance, typed from tenon.config.ts:

src/pages/posts/[slug].astro
---
const post = await Astro.locals.tenon.findOne("posts", {
  where: { slug: Astro.params.slug! },
});
if (!post) return new Response(null, { status: 404 });
---

<h1>{post.title}</h1>
  • On a page that renders per request, it belongs to the user of the request. An editor in the preview reads drafts, and the access rules apply. Everyone else reads published content.
  • On a prerendered page, it is the plain instance. The page reads published content once, at build time, so the build needs the database.
  • Endpoints in src/pages/ get the same instance as context.locals.tenon.

See Reading content for the read methods.

Render and preview

The components of tenoncms/astro/components render fields as HTML: Image, Picture, Link, Blocks, Richtext and Seo. This page assumes a cover image field and a rich text body:

src/pages/posts/[slug].astro
---
import { Image, Richtext, Seo } from "tenoncms/astro/components";
import { edit } from "tenoncms/preview";

const post = await Astro.locals.tenon.findOne("posts", {
  where: { slug: Astro.params.slug! },
});
if (!post) return new Response(null, { status: 404 });
---

<html lang="en">
  <head>
    <Seo page={post} />
  </head>
  <body>
    <h1 data-cms={edit("posts", post.id, "title")}>{post.title}</h1>
    <Image image={post.cover} sizes="(min-width: 720px) 720px, 100vw" />
    <Richtext content={post.body} />
  </body>
</html>
  • Seo reads the general and seo settings itself when you do not pass them. See Components.
  • The integration adds the live preview to every page. Mark what a field renders with edit(). After each save, the page renders again in the background. See Live preview.
  • With localization, Astro.locals.locale holds the language of the URL and Astro.locals.path the path without the language prefix.

Integration options

Pass the options to tenonCms():

astro.config.ts
integrations: [tenonCms({ jobs: false })],
The file that exports the CMS instance, relative to the project root. Default: "./tenon.config.ts".Adds the live preview and the admin bar to every page. Default: true.Serves /sitemap.xml from the CMS. Default: true. Your own src/pages/sitemap.xml.ts or public/sitemap.xml wins. See Sitemap and robots.txt.Serves /robots.txt from the CMS. Default: true. Your own route or public/robots.txt wins.Runs due jobs every minute. Default: true. The timer runs with @astrojs/node and in astro dev. On Cloudflare, the Durable Object runs them. On Vercel, nothing does. See The clock.

The integration reads admin.path from tenon.config.ts at startup. Restart the dev server after you change it. See Admin path.

Deploy

Node

@astrojs/node on a server or in a container.

Vercel

@astrojs/vercel, with Postgres or Turso and a bucket.

Cloudflare

@astrojs/cloudflare, with the CMS in a Durable Object.