Seed Content
A seed fills a new database once. A fresh clone, a new teammate or a starter then opens with pages, posts and images instead of an empty admin. This recipe ships a content archive with your project and imports it on the first start.
Before you start
- You have a running site with Tenon and a few collections. See Quick Start.
- Lifecycle hooks explains when a seed runs. This recipe does not repeat it.
Ship an archive
Build the content
Start the dev server and create the content in the admin: documents, globals and media files. Publish what visitors should see. A draft stays a draft after the import.
Download a content archive
Open Manage → Backup in the admin. Keep Include media files checked and click Download ZIP under Export.
The archive holds documents, globals, translations, versions and media files. It holds no users, so every clone creates its own first admin.
Add it to the project
Save the file as seed/content.zip and commit it. Media files make the archive large, so keep the sample images small.
Import it in seed
import { openAsBlob } from "node:fs";
import { defineCms } from "tenoncms";
export default defineCms({
// ...
seed: async ({ cms }) => {
await cms.import(await openAsBlob("seed/content.zip"));
},
});
- The path is relative to the working directory of the server.
nuxt previewruns from.output/, so pass an absolute path there. - Use the
cmsthe seed gets. The instance fromdefineCms()waits for the seed to finish, so a call to it from the seed never returns.
Try it on a fresh database
Stop the dev server and point DATABASE_URL (NUXT_DATABASE_URL in Nuxt) at a new file, like file:./fresh.db. Start the server again and open the admin. Create the first admin: the content from the archive is there.
seed later is not seeded.Create content in code
A seed can also write content itself. This suits a few documents that change with the code, like a home page and a menu:
import { readFile } from "node:fs/promises";
import { defineCms } from "tenoncms";
export default defineCms({
// ...
seed: async ({ cms }) => {
const hero = await cms.media.upload(await readFile("seed/hero.jpg"), {
folder: "pages",
filename: "hero.jpg",
mimeType: "image/jpeg",
});
await cms.media.update(hero.id, { alt: "Harbour at dusk" });
await cms.create("posts", { title: "Welcome", hero: hero.id });
await cms.setGlobal("general", { title: "My Site" });
},
});
cms.create()andcms.setGlobal()publish at once. Pass{ draft: true }to save a draft. See Writing.- The
cmsof a seed skips the access rules and is untyped. - If the seed throws, nothing it wrote is kept, and it runs again on the next start. Files it uploaded stay in storage.
Seed a site that already runs
A seed reads its file from the server's disk. Cloudflare Workers have no node:fs, and some hosts do not deploy the file next to the server. There, restore the archive into the running site with the CLI:
npx tenon login https://example.com
npx tenon restore seed/content.zip --url https://example.com
tenon restore.Run code on every start
onInit runs on every start, after the seed. Serverless hosts start often, sometimes for each request, so keep it fast and safe to run many times. See onInit.
Related resources
- Lifecycle hooks:
seed,onInit,afterImportandafterError. - Backup and restore: content archives, import modes and moving content.
- Media: upload files from code.
- Write a plugin: a plugin's seed installs its defaults.