Headless CMS for a blog
Model posts and authors, let editors manage content in the panel, serve published posts to your website and trigger a rebuild on every change.
In this recipe you build the content backend of a blog: editors write in the panel, the website reads published posts, and every change triggers a rebuild of the static site.
1. Model the content
Create two buckets. With the CLI, add these files and run spica apply:
title: Authors
description: People who write posts
primary: name
properties:
name:
type: string
title: Name
bio:
type: textarea
title: Bio
options:
translate: true
avatar:
type: storage
title: Avatar
required:
- nameApply it, note the bucket id of Authors (spica fetch writes it into the file), and create the posts bucket that references it:
title: Posts
description: Articles published on the blog
primary: title
history: true
properties:
title:
type: string
title: Title
options:
translate: true
slug:
type: string
title: Slug
pattern: ^[a-z0-9-]+$
body:
type: richtext
title: Body
options:
translate: true
status:
type: string
title: Status
enum: [draft, published]
default: draft
author:
type: relation
title: Author
bucketId: <Authors bucket id>
relationType: onetoone
cover:
type: storage
title: Cover image
published_at:
type: date
title: Published at
required: [title, slug]
indexes:
- definition: {slug: 1}
options: {unique: true}
- definition: {status: 1, published_at: -1}
acl:
read: document.status == "published"
write: "false == true"history: true lets editors restore earlier versions. The unique index keeps slugs unique, and the second index serves the website's main query.
2. Give editors access
Create a policy for editors:
name: Content Editor
description: Manage posts and authors, upload media
statement:
- {action: bucket:index, module: bucket, resource: {include: ["*"], exclude: []}}
- {action: bucket:show, module: bucket, resource: {include: [<postsId>, <authorsId>], exclude: []}}
- {action: bucket:data:index, module: bucket:data, resource: {include: [<postsId>, <authorsId>], exclude: []}}
- {action: bucket:data:show, module: bucket:data, resource: {include: [<postsId>, <authorsId>], exclude: []}}
- {action: bucket:data:create, module: bucket:data, resource: {include: [<postsId>, <authorsId>], exclude: []}}
- {action: bucket:data:update, module: bucket:data, resource: {include: [<postsId>, <authorsId>], exclude: []}}
- {action: bucket:data:delete, module: bucket:data, resource: {include: [<postsId>, <authorsId>], exclude: []}}
- {action: preference:show, module: preference, resource: {include: [bucket], exclude: []}}Create an identity for each editor and attach Content Editor and StorageFullAccess. Editors now see only the two buckets in the panel.
3. Read published posts on the website
The website renders on a server, so it can use an API key. Create a key named Website with a read-only policy for the two buckets (bucket:data:index and bucket:data:show).
import * as Bucket from "@spica-devkit/bucket";
Bucket.initialize({apikey: process.env.SPICA_API_KEY!, publicUrl: process.env.SPICA_URL!});
const POSTS = process.env.POSTS_BUCKET_ID!;
export function listPosts(language: string, page = 0) {
return Bucket.data.getAll(POSTS, {
queryParams: {
filter: 'document.status == "published"',
sort: {published_at: -1},
limit: 10,
skip: page * 10,
paginate: true,
relation: ["author"]
},
headers: {"Accept-Language": language}
});
}
export async function getPost(slug: string, language: string) {
const [post] = await Bucket.data.getAll(POSTS, {
queryParams: {filter: `document.status == "published" && document.slug == "${slug}"`, limit: 1, relation: ["author"]},
headers: {"Accept-Language": language}
});
return post;
}Validate slug (for example against ^[a-z0-9-]+$) before you put it into a filter.
4. Rebuild the site on every change
Static site hosts give you a "deploy hook" URL. Create a webhook per operation type that calls it:
{
"title": "Rebuild site on post update",
"url": "https://api.netlify.com/build_hooks/<hook id>",
"body": "{\"trigger\": \"{{type}} {{documentKey}}\"}",
"trigger": {"name": "database", "active": true, "options": {"collection": "bucket_<postsId>", "type": "UPDATE"}}
}Repeat it for INSERT and DELETE. Check deliveries in the webhook's logs.
5. Localize
The bucket preferences define the languages. Editors fill in each language in the panel, and the site sends the visitor's language in Accept-Language. Spica answers with the matching translation or the default language.
Result
- Editors manage posts and authors in the panel, with history and image uploads.
- The site reads only published posts, in the visitor's language, with author details resolved.
- Every change triggers a rebuild.