Data
Three options for sourcing structured data in zfb — content collections, data collections, and plain TS modules.
zfb gives you three different routes for getting structured data into a page, and the right choice depends on the shape of the data and how much ceremony you want around it.
1. Content collections
When the data is prose — blog posts, docs articles, recipes — use a content collection. Each entry is a Markdown file with frontmatter, the frontmatter is validated against a schema, and the body is rendered to HTML for you. See Content Collections for the full story.
This is the right choice whenever the body of each entry is meant to be read.
2. Data collections
When the data is structured — a list of products, a directory of team members, a set of pricing tiers — use a data collection: a directory of JSON / YAML / TOML files declared in zfb.config.ts. The same [{ name, path }] shape that content collections use (see Content Collections) accepts data directories without ceremony:
export default {
collections: [
{
name: "products",
path: "data/products",
},
],
};
Each file becomes one entry. You can supply an optional schema field with per-field validators (see <code>defineConfig</code> for the full shape). You load data entries the same way as content entries:
import { getCollection } from "zfb/content";
const products = getCollection("products");
A data collection earns its keep when you want per-entry validation, slug derivation, and the same loading API as your prose content — without the Markdown-rendering step.
3. Plain TS modules under data/
For everything else — small lookups, helper functions, derived constants — a regular TypeScript module is the lightest path:
// data/site.ts
export const siteName = "My Site";
export function formatDate(date: Date) {
return date.toISOString().slice(0, 10);
}
Import it from any page or component:
import { siteName, formatDate } from "../data/site";
There is no schema, no slug, and no per-entry overhead. Use this when the “collection” framing would be more ceremony than the data deserves.
Picking the right tool
A useful rule of thumb:
- The data is content meant to be read → content collection.
- The data is structured records that share a shape → data collection.
- The data is a handful of constants or a helper → TS module under
data/.
You can mix all three in the same project without conflict.