Frameworks

TanStack Start

Set up Tenon in a TanStack Start project.

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

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

Add the Vite plugin

tenonAdmin() builds the admin and runs the job clock:

vite.config.ts
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:

src/routes/[_]tenon.$.ts
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:

tsconfig.json
{
  "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:

tenon.config.ts
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:

.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:

src/routes/posts/$slug.tsx
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:

  1. Call preview() once in the root route. For visitors without a session, it does nothing.
    src/routes/__root.tsx
    import { useEffect } from "react";
    import { preview } from "tenoncms/preview";
    
    function Root() {
      useEffect(() => preview(), []);
      // ...
    }
    
  2. Pass the loader data through useDraft(). It merges unsaved changes, so the whole page follows while the editor types:
    src/routes/posts/$slug.tsx
    import { 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

The file that exports the CMS instance, relative to the project root. Default: "./tenon.config.ts".Runs due jobs every minute through the Nitro task tenon:jobs. Default: true. On Vercel, Nitro turns the task into a Vercel Cron Job. See The clock.
  • tenonAdmin() throws when tenon.config.ts sets admin: false. Remove the plugin and the admin route then.
  • It reads admin.path at startup. For another path, like /cms, rename the admin route to src/routes/cms.$.ts and pass the path to preview({ admin: "/cms" }). See Admin path.
  • It keeps tenoncms out of the server bundle, because bundling it breaks the native driver of libSQL.
Vercel Hobby allows one cron job a day, and a deploy with a cron every minute fails. The Vercel starter sets tenonAdmin({ jobs: false }). Call the clock route from a cron service instead, or remove jobs: false on the Pro plan.

Deploy

Node

vite build and node .output/server/index.mjs.

Vercel

Vercel functions, with Postgres or Turso and a bucket.