Skip to content

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.

NameTakesReturnsMore in
lazyvalueA function that resolves value when calledLazy blocks
multivariatevariants: a list of { rule, value }The first variant whose rule is trueMatchers and variants
alwaysnothingtrueMatchers and variants
nevernothingfalseMatchers and variants
datestart, endtrue from start until endMatchers and variants
pagename, path, seo, sectionsThe page, fully resolvedPages and routing
redirectfrom, to, permanent, status, discardQueryParametersIts arguments, as savedPages and routing
cms-settingspreview, telemetry, analyticsIts 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:

  • lazy takes value and returns a Lazy<T> function that resolves it when called, at most once. It's the one block whose argument doesn't resolve first (see Lazy blocks).
  • multivariate takes variants, a list of entries that each pair a rule (a boolean) with a value (a Lazy<T>), and returns the first variant whose rule is true, or undefined if none is. Only that variant runs. It also takes an optional experiment, the stable ID of an A/B test.
  • always returns true, so the variant paired with it is the fallback.
  • never returns false, so the variant paired with it is never picked. That's how the site editor hides a block: it resolves to undefined, and a list leaves it out (see Hide a block).
  • date takes start and end, both optional ISO 8601 strings, and returns true from start until end.

Matchers and variants shows how they work together, and how to write matchers of your own.

Pages and redirects

  • page takes name, path, an optional seo and sections (the page's blocks, in order), and returns the page with seo and every block in sections resolved. Its type is interface Page extends Route { seo?: Seo; sections: ReactNode[] }. A page without seo uses your site's defaults.
  • redirect takes from, to, permanent (true for a 301, false for a 302), and two optional fields: status, one of 301, 302, 307 or 308, which wins over permanent, and discardQueryParameters, 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.

.deco/blocks/CMS.json
{
  "__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. createCMS holds 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 CMS block 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:

SectionFieldsDefaultsMore in
previewhostsEvery host, or the hosts code allowsAllow previews per host
telemetryenabled, metrics, errorSampleRate, traceSampleRatetrue, true, 0.05, 0Telemetry settings are content
analyticsenabled, collectortrue, the hosted Deco CMS collectorAnalytics

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:

  1. 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.
  2. Read it in your site's code, where you create the client that needs it, and pass it as a parameter.
  3. Keep only what editors choose in content, such as who receives the messages.
src/email.ts
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! });
src/contact-form.tsx
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:

.deco/index.ts
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).