TanStack Start
TanStack Start needs no module. tenonAdmin() in the Vite config builds the admin, and four route files hand requests to the CMS. Server functions read content.
Create a project
Run the wizard and pick TanStack Start:
npm create tenoncms@latest
pnpm create tenoncms
yarn create tenoncms
bun create tenoncms
The starter has vite.config.ts with tenonAdmin(), tenon.config.ts, a posts collection, the API and admin routes, and two pages. Start it with npm run dev, open the URL it prints and add /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
Tenon needs a server build, so add Nitro too:
npm install tenoncms @libsql/client
npm install -D nitro
pnpm add tenoncms @libsql/client
pnpm add -D nitro
yarn add tenoncms @libsql/client
yarn add -D nitro
bun add tenoncms @libsql/client
bun add -D nitro
Add the Vite plugin
tenonAdmin() builds the admin and runs the job clock:
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import viteReact from "@vitejs/plugin-react";
import { nitro } from "nitro/vite";
import { tenonAdmin } from "tenoncms/vite";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [tanstackStart(), nitro(), viteReact(), tenonAdmin()],
});
Add the server routes
The API route hands every request method to the CMS. The admin route serves the admin page. The sitemap and robots.txt routes are optional:
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 brackets escape characters that TanStack Router reads as syntax. A file named _tenon.$.ts is a pathless layout route and never matches /_tenon.
Register the types
Add tenoncms/register to types, and point #tenon/config at your config:
{
"compilerOptions": {
"types": ["vite/client", "node", "tenoncms/register"],
"paths": { "#tenon/config": ["./tenon.config.ts"] }
}
}
This types #tenon/admin-html, and createClient() from tenoncms/client without a type argument.
Create the CMS instance
Create tenon.config.ts at the project root:
import { defineCms } from "tenoncms";
import { libsql } from "tenoncms/libsql";
import { posts } from "./cms/collections/posts";
export default defineCms({
collections: { posts },
database: libsql(process.env.DATABASE_URL ?? "file:./cms.db"),
secret: process.env.TENON_SECRET!,
site: { url: process.env.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, open the URL it prints and add /admin. The first account you create is the admin.
Read content
Loaders run on the server and in the browser, but the CMS runs only on the server. Read content in a server function, and call it from the loader:
import { createFileRoute, notFound } from "@tanstack/react-router";
import { createServerFn } from "@tanstack/react-start";
import { getRequest } from "@tanstack/react-start/server";
import cms from "../../../tenon.config";
const getPost = createServerFn()
.validator((slug: string) => slug)
.handler(async ({ data: slug }) => {
const tenon = await cms.withSession(getRequest());
const post = await tenon.findOne("posts", { where: { slug } });
if (!post) throw notFound();
return post;
});
export const Route = createFileRoute("/posts/$slug")({
loader: ({ params }) => getPost({ data: params.slug }),
component: Post,
});
function Post() {
const post = Route.useLoaderData();
return <h1>{post.title}</h1>;
}
cms.withSession(getRequest()) reads as the user of the request. An editor in the preview reads drafts, and the access rules apply. The imported cms skips both, so use it only for content every visitor may see. See Drafts.
Render and preview
The components of tenoncms/react render fields as HTML: Image, Picture, Link, Blocks, Richtext and Seo. They use no hooks, so they also work as server components. See Components.
The live preview needs two additions:
- Call
preview()once in the root route. For visitors without a session, it does nothing.src/routes/__root.tsximport { useEffect } from "react"; import { preview } from "tenoncms/preview"; function Root() { useEffect(() => preview(), []); // ... } - Pass the loader data through
useDraft(). It merges unsaved changes, so the whole page follows while the editor types:src/routes/posts/$slug.tsximport { edit } from "tenoncms/preview"; import { useDraft } from "tenoncms/preview/react"; function Post() { const post = useDraft("posts", Route.useLoaderData()); return <h1 data-cms={edit("posts", post.id, "title")}>{post.title}</h1>; }
edit() marks what a field renders. See Live preview.
TanStack Start has no adapter for languages. Read the language from the URL with localeFromPath() in a server function. See Localization.
tenonAdmin() options
"./tenon.config.ts".tenon:jobs. Default: true. On Vercel, Nitro turns the task into a Vercel Cron Job. See The clock.tenonAdmin()throws whentenon.config.tssetsadmin: false. Remove the plugin and the admin route then.- It reads
admin.pathat startup. For another path, like/cms, rename the admin route tosrc/routes/cms.$.tsand pass the path topreview({ admin: "/cms" }). See Admin path. - It keeps
tenoncmsout of the server bundle, because bundling it breaks the native driver of libSQL.
tenonAdmin({ jobs: false }). Call the clock route from a cron service instead, or remove jobs: false on the Pro plan.Deploy
Related resources
- Add to an Existing Site: the full setup, with a first page.
- Configuration:
tenon.config.tsand the environment variables. - Pages and SEO: head tags with
seoHead()in a route'shead(). - Live preview: mark elements and follow unsaved changes.
- Other frameworks: the same setup in other Vite apps.