Built-in blocks
Eight functions every block map gets for free (lazy, multivariate, always, never, date, page, redirect and cms-settings), how to replace or change one, and where secrets go instead.
Almost every site needs pages, redirects and an if/else. Instead of writing those functions yourself, Deco CMS adds eight of them to your block map. You don't import or list them, and they're always in the schema and the registry, so a built-in never fails with UNKNOWN_BLOCK.
This page lists the built-ins, what each one takes and returns, and how to replace one or change the fields editors see. It's a quick list: each built-in is explained on the page where you'll use it, linked in the last column.
| Name | Takes | Returns | More in |
|---|---|---|---|
lazy | value | A function that resolves value when called | Lazy blocks |
multivariate | variants: a list of { rule, value } | The first variant whose rule is true | Matchers and variants |
always | nothing | true | Matchers and variants |
never | nothing | false | Matchers and variants |
date | start, end | true from start until end | Matchers and variants |
page | name, path, seo, sections | The page, fully resolved | Pages and routing |
redirect | from, to, permanent, status, discardQueryParameters | Its arguments, as saved | Pages and routing |
cms-settings | preview, telemetry, analytics | Its arguments, with defaults filled in (telemetry's sample rates only as saved: cms.settings() fills them from code's defaults) | CMS settings below |
Language built-ins
These five are part of how blocks evaluate, so every site gets the same ones:
lazytakesvalueand returns aLazy<T>function that resolves it when called, at most once. It's the one block whose argument doesn't resolve first (see Lazy blocks).multivariatetakesvariants, a list of entries that each pair arule(aboolean) with avalue(aLazy<T>), and returns the first variant whose rule istrue, orundefinedif none is. Only that variant runs. It also takes an optionalexperiment, the stable ID of an A/B test.alwaysreturnstrue, so the variant paired with it is the fallback.neverreturnsfalse, so the variant paired with it is never picked. That's how the site editor hides a block: it resolves toundefined, and a list leaves it out (see Hide a block).datetakesstartandend, both optional ISO 8601 strings, and returnstruefromstartuntilend.
Matchers and variants shows how they work together, and how to write matchers of your own.
Pages and redirects
pagetakesname,path, an optionalseoandsections(the page's blocks, in order), and returns the page withseoand every block insectionsresolved. Its type isinterface Page extends Route { seo?: Seo; sections: ReactNode[] }. A page withoutseouses your site's defaults.redirecttakesfrom,to,permanent(truefor a 301,falsefor a 302), and two optional fields:status, one of301,302,307or308, which wins overpermanent, anddiscardQueryParameters, which drops the request's query string instead of carrying it over. It returns them as saved.
Pages and routing shows how to find the page for a URL and turn redirects into responses.
CMS settings
Marketing wants page views to go to their own collector, and the team wants drafts to open only on the staging host. Both are site-wide settings editors can see and change, so they live in one place: a saved block named CMS, whose type is the built-in cms-settings.
{
"__resolveType": "cms-settings",
"preview": { "hosts": ["staging.example.com"] },
"telemetry": { "enabled": true, "metrics": true, "errorSampleRate": 0.05, "traceSampleRate": 0 },
"analytics": { "enabled": true, "collector": "https://stats.example.com/events" }
}Settings are split between code and content:
- Code says where things go, and how far content may go.
createCMSholds destinations and credentials, such as your telemetry collector and its token, the default sample rates, and the cap on the hosts content may allow previews on. Code changes with a deploy. - Content says what's on, and how much. The
CMSblock holds the switches, the sample rates (which win over code's defaults), the analytics collector and the preview hosts (within code's cap). It ships like any other content, and editors change it in the site editor.
The block has three sections, each with its own page:
| Section | Fields | Defaults | More in |
|---|---|---|---|
preview | hosts | Every host, or the hosts code allows | Allow previews per host |
telemetry | enabled, metrics, errorSampleRate, traceSampleRate | true, true, 0.05, 0 | Telemetry settings are content |
analytics | enabled, collector | true, the hosted Deco CMS collector | Analytics |
Every field is optional, and so is the block: without it, every default applies. deco content doesn't create it; save it from the site editor, or add the file, when you want to change something. Like any field, a section or a single setting can have variants, such as analytics switched on only during a campaign.
Your code reads the settings with cms.settings(), which returns every section with the defaults filled in and the preview hosts capped. It always reads the release, never a draft, so a draft can't allow its own preview host or change what telemetry sends. Telemetry, analytics and the draft helpers all read the settings this way.
Secrets
A contact form sends each message by email, through Resend, which needs an API key. That key doesn't belong in content: content lives in your repository, ships in every release, and editors can read every field. So there's no secret block. Secrets live in your deploy platform's secrets, and your code passes them on:
- Store the value in your deploy platform's secrets. On Cloudflare Workers, that's
npx wrangler secret put RESEND_API_KEY; other hosts have a secrets or environment setting of their own. - Read it in your site's code, where you create the client that needs it, and pass it as a parameter.
- Keep only what editors choose in content, such as who receives the messages.
import { createResendClient } from "@decocms/apps-resend";
// Created once, with the key from your deploy platform's secrets.
export const resend = createResendClient({ apiKey: process.env.RESEND_API_KEY! });export interface ContactFormProps {
/** @title Send messages to */
to: string;
subject: string;
}The block's form shows the recipient and the subject, and the server function that sends a message imports resend. Changing the key is a change to your platform's secrets, which takes effect the way your platform applies new secrets, usually with the next deploy.
A v7 site kept some credentials in content, as website/loaders/secret.ts blocks. The migration lists each one, so you can move it to your platform's secrets (see Secrets). deco check points out any that are left.
Change a built-in
Your block map is spread over the built-ins (see the lookup rule), so declaring a built-in's key in your block map replaces it. For example, declare page to wrap every page in your layout, or declare multivariate to pick variants your way (type each value as Lazy<T> to keep running only the chosen one).
To change only the fields editors see, declare the key with a function that returns its input. Page is exported from @decocms/blocks; extend it to add fields:
import type { Blocks, Page } from "@decocms/blocks";
import seo from "../src/seo";
import hero from "../src/hero";
interface StorePage extends Page { theme: "light" | "dark" }
const page = (props: StorePage) => props;
export default { seo, hero, page } satisfies Blocks;Nothing is lost: arguments resolve before the function runs, so returning props gives the same resolved page as the built-in, seo and sections included, plus theme. You read it in the result of client.resolve, as page.theme, and deco schema gives editors a theme select on every page.
No saved block can be called page, redirect, cms-settings, always, never, date, multivariate or lazy (see Names).