Nuxt
The tenoncms/nuxt module adds the API, the admin and the live preview to your Nuxt app. Pages read content with useTenon(), and the Tenon components are auto-imported.
Create a project
Run the wizard and pick Nuxt:
npm create tenoncms@latest
pnpm create tenoncms
yarn create tenoncms
bun create tenoncms
The starter has nuxt.config.ts with the module and runtimeConfig, tenon.config.ts, a posts collection and two pages. Start it with npm run dev, open http://localhost:3000/admin and create the first admin. See create-tenoncms for every question and flag.
Add to an existing project
This setup uses a SQLite file. For the other databases, see Databases.
Install the packages
npm install tenoncms @libsql/client
pnpm add tenoncms @libsql/client
yarn add tenoncms @libsql/client
bun add tenoncms @libsql/client
Add the module
Add tenoncms/nuxt to modules, and declare the values in runtimeConfig:
import { resolve } from "node:path";
export default defineNuxtConfig({
modules: ["tenoncms/nuxt"],
runtimeConfig: {
tenonSecret: "",
tenonSiteUrl: "",
databaseUrl: `file:${resolve("cms.db")}`,
},
});
Each key reads its NUXT_ variable: databaseUrl reads NUXT_DATABASE_URL. The database path is absolute, because nuxt preview runs the server from inside .output/. Do the same for a local() media folder.
Create the CMS instance
Create tenon.config.ts at the project root. It reads the values with useRuntimeConfig():
import { defineCms } from "tenoncms";
import { libsql } from "tenoncms/libsql";
import { posts } from "./cms/collections/posts";
const config = useRuntimeConfig();
export default defineCms({
collections: { posts },
database: libsql(config.databaseUrl),
secret: config.tenonSecret,
site: { url: config.tenonSiteUrl || undefined, name: "My Site" },
});
The tenon CLI loads nuxt.config.ts, so useRuntimeConfig() returns the same values there. For the posts collection, see Add a collection.
Set the secret
Generate a secret with openssl rand -hex 32 and put it in .env:
NUXT_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:3000/admin. The first account you create is the admin.
Read content
useTenon() has the read methods find, findOne, findById and getGlobal. Each one returns the result of useAsyncData:
<script setup lang="ts">
const route = useRoute();
const { data: post } = await useTenon().findOne("posts", () => ({
where: { slug: String(route.params.slug) },
}));
</script>
<template>
<h1 v-if="post">{{ post.title }}</h1>
</template>
- During server rendering, it calls the CMS in the same process. On a client-side navigation, it fetches the public read API.
- It reads with the cookies of the request. An editor in the preview reads drafts. Everyone else reads published content.
- Options and ids take refs or getters. Use a getter for options that depend on the route, as above, so a reused page updates on navigation.
- With localization, it reads in the language of the route.
Server routes import the instance from #tenon/config:
import cms from "#tenon/config";
export default defineEventHandler(() => cms.find("posts", { limit: 5 }));
The imported instance skips the access rules. For the user of the request, call await cms.withSession(toWebRequest(event)). See Reading content.
Render and preview
The module auto-imports the components of tenoncms/vue with the prefix Tenon: TenonImage, TenonPicture, TenonLink, TenonBlocks and TenonRichtext. TenonLink renders internal links with NuxtLink. This page assumes a cover image field and a rich text body:
<script setup lang="ts">
import { edit } from "tenoncms/preview";
const route = useRoute();
const { data: post } = await useTenon().findOne("posts", () => ({
where: { slug: String(route.params.slug) },
}));
await useTenonSeo(post);
</script>
<template>
<article v-if="post">
<h1 :data-cms="edit('posts', post.id, 'title')">{{ post.title }}</h1>
<TenonImage :image="post.cover" sizes="(min-width: 720px) 720px, 100vw" />
<TenonRichtext :content="post.body" />
</article>
</template>
useTenonSeo()puts the title, description, Open Graph andhreflangtags into<head>. See Pages and SEO.- The module starts the live preview on every page.
useTenon()merges unsaved changes into itsdata, so the whole page follows while the editor types. See Live preview. useLivePreview()tells whether the page runs in the admin's preview.useBlockEdit()marks blocks for click-to-edit. See Blocks.useLocale()anduseAlternates()build language switches. See Localization.
Module options
Set the options under tenonCms in nuxt.config.ts:
export default defineNuxtConfig({
modules: ["tenoncms/nuxt"],
tenonCms: { jobs: false },
});
"./tenon.config.ts"./sitemap.xml from the CMS. Default: true. Your own server/routes/sitemap.xml.ts, a file in public/ or the @nuxtjs/sitemap module wins. See Sitemap and robots.txt./robots.txt from the CMS. Default: true. Your own route, a file in public/ or the @nuxtjs/robots module wins.tenon:jobs. Default: true. See The clock.jobs is on. Set tenonCms: { jobs: false } and call the clock route from a cron service. The Vercel starter does this for you.The module reads admin.path from tenon.config.ts at startup. Restart the dev server after you change it, and when a new browser module for the admin does not show. 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 follow unsaved changes.
- Localization: languages in the URL.