Drafts and publishing
Manage draft revisions, publish workflows, and unpublish operations across collections in Xenon.
Draft lifecycle
You can draft edits privately, preview unpublished changes, and publish documents when ready.
import { defineCollection, text } from "xenon-cms";
export const articles = defineCollection({
slug: "articles",
drafts: true, // enabled by default
fields: {
title: text({ required: true }),
content: text({ required: true }),
},
});When drafts: true is active, every document tracks a lifecycle status of either "draft" or "published".
How drafts work
Xenon isolates draft revisions from published content at the database level:
┌────────────────────────────────────────────────────────┐
│ Document Database │
├──────────────────────────┬─────────────────────────────┤
│ Published Data (data) │ Draft Data (draftData) │
│ Live on public site │ Staged working copy │
├──────────────────────────┴─────────────────────────────┤
│ Status: "published" (live) or "draft" (unpublished) │
└────────────────────────────────────────────────────────┘- Saving drafts: Writing with
draft: truesaves attributes intodraftDatawithout altering livedata. Validation runs in draft mode:requiredconstraints are relaxed, allowing partial or unfinished entries. - Publishing: Publishing validates all required fields against create rules. The working draft merges into
data,draftDatais cleared tonull, andstatusupdates to"published". - Unpublishing: Calling
unpublishmoves currentdataintodraftData(if no newer draft exists) and marksstatusas"draft". The document immediately disappears from public queries.
Querying draft content
Control whether your queries return live published records or staging drafts.
import { getXenon } from "xenon-cms/next";
import config from "@/xenon.config";
export default async function PreviewPage({ params }: { params: Promise<{ slug: string }> }) {
const { slug } = await params;
// Reads published records only by default
const liveDoc = await getXenon(config).findBySlug({
collection: "articles",
slug,
});
// Include unpublished drafts and staged revisions
const draftDoc = await getXenon(config).findBySlug({
collection: "articles",
slug,
draft: true,
});
return <div>{draftDoc?.title} (Status: {draftDoc?.status})</div>;
}When draft: true is supplied, field values resolve from draftData ?? data.