Astro
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
pnpm create tenoncms
yarn create tenoncms
bun create tenoncms
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
pnpm add tenoncms @libsql/client @astrojs/node
yarn add tenoncms @libsql/client @astrojs/node
bun add tenoncms @libsql/client @astrojs/node
Add the integration
Add the adapter and tenonCms(), and declare the variables in the env schema:
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:
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:
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:
---
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 ascontext.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:
---
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>
Seoreads thegeneralandseosettings 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.localeholds the language of the URL andAstro.locals.paththe path without the language prefix.
Integration options
Pass the options to tenonCms():
integrations: [tenonCms({ jobs: false })],
"./tenon.config.ts".true./sitemap.xml from the CMS. Default: true. Your own src/pages/sitemap.xml.ts or public/sitemap.xml wins. See Sitemap and robots.txt./robots.txt from the CMS. Default: true. Your own route or public/robots.txt wins.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
Related resources
- Add to an Existing Site: the full setup, with a first page.
- Configuration:
tenon.config.tsand the environment variables. - Components: every component and its props.
- Live preview: mark elements and prerendered pages.
- Localization: languages in the URL.