Synced blocks

Edit a block once and place it on many pages with @cms/synced-blocks, then swap the placements for the block on your website.

@cms/synced-blocks is a plugin for blocks edited once and placed on many pages: one shipping notice, one FAQ, one video, shown on every product that needs it. It adds:

  • a Synced blocks collection: each document holds one block, with the fields it has on a page
  • synced.block(), a pointer a page’s union holds in the block’s place
  • the checks that keep a synced block renderable wherever it’s placed
  • unwrapSyncedBlocks() for your website, which swaps each pointer for its block
          pnpm add @cms/synced-blocks
        
          // cms.config.tsx
import { array, config, entity, union } from "@cms/config";
import { syncedBlocks } from "@cms/synced-blocks";

const synced = syncedBlocks({ blocks: [faq(), video(), shipping()] });

const page = entity("page", {
  fields: [array("blocks", { of: union("block", { of: [hero(), faq(), video(), synced.block()] }) })],
});

const productPage = entity("product_page", {
  fields: [
    array("blocks", { of: union("block", { of: [video(), shipping(), faq(), synced.block()] }) }),
  ],
});

export default config({ entities: [page, productPage], plugins: [synced] });
        

Run cms migration generate, then cms migration apply, to create the table.

Options

Option Type Description
blocks ObjectField[] What a synced block can hold. Required.
entity string The collection’s name, which is also the pointer’s _type. Default: "synced_block".
translatable boolean A synced block per locale, like the pages that place it. Default: false. See Translations.
hooks EntityHooks More hooks on the collection. Its before-hooks run before the plugin’s checks, which see what they return.
name string The instance name. Default: "synced-blocks".

Leave out of blocks any block that reads from the page it sits on (reviews of the page’s product, a grid of its variants): a synced block renders the same everywhere.

The plugin adds the collection but no permissions on it: grant roles read, create, update and delete on it, as for your own entities. Your website’s key needs read.

Placing synced blocks

synced.block() is the pointer: a block holding one relation, source, to a synced block. Put it in the of of every union that may show synced blocks. Each call makes a new pointer, so call it once per union.

Option Description
title, description What the picker shows. Default: “Synced block”.

In a blocks() field, its icon, thumbnail and search words go in variants, under the pointer’s _type:

          import { blocks } from "@cms/blocks";
import { ArrowsClockwiseIcon } from "@phosphor-icons/react";

blocks("block", {
  of: [hero, faq, synced.block()],
  variants: { synced_block: { icon: ArrowsClockwiseIcon, keywords: { fr: ["Bloc partagé"] } } },
});
        

config() throws when a pointer’s instance isn’t in plugins, and the server won’t start with a pointer inside rich text (in a rich-text block’s fields), where the checks below can’t run.

What each page may hold

A union holding a pointer allows the block types in its own of, so nothing is declared twice. On the example above, a synced shipping block can be placed on a product page and not on a page.

When Refused if The error shows on
A page is saved, as a draft or published The synced block a pointer names holds a type the union doesn’t, in any translation, or no longer exists The pointer’s Synced block field
A synced block is created, saved or restored A page placing it, through any translation, sits in a union that can’t hold its new type Its Block field

Both read the latest draft and the published version, since either can be live. A synced block can’t hold a pointer, because synced.block() is never one of blocks. The second error names the page only when the editor can read it; otherwise it says “a document you can’t open”.

On your website

blocks on a page read keeps its pointers: { _type: "synced_block", source: { id, _entity } }. unwrapSyncedBlocks swaps each one for the block its synced block holds, in one request:

          import { unwrapSyncedBlocks } from "@cms/synced-blocks/client";

const { data: page } = await cms.get("page", id, { resolve });
const blocks = await unwrapSyncedBlocks(cms, page.blocks, {
  draft,
  resolve: { block: { image: { url: true, alt: true } } },
});
// Renderers see ordinary blocks, typed as the page's own and the shareable ones.
        
Option Description
entity The collection’s name, when you renamed it.
locale The page’s locale, for translatable synced blocks.
draft true reads pending drafts and unpublished synced blocks, for a preview. Anything else, the client’s own draft default included, reads only published ones.
resolve The synced block’s resolve tree, { block: { … } }: the relations inside its blocks.

A pointer whose synced block is missing, unpublished (outside draft) or not translated into locale is dropped, so the page still renders. The read is a request of its own, so resolve isn’t capped by the page’s resolve depth.

Translations

On a site whose pages are translatable, set translatable: true. Without it a synced block is a single document, so every locale’s pages show the same text.

With translatable: true, each synced block has a document per locale and a pointer may name any of them: pass locale to unwrapSyncedBlocks and each pointer shows the one in that locale. Translating a page remaps its pointers to the synced blocks in the new locale where they exist.

Unwrapping finds a pointer’s translation by its translation group, which the pointer stores: the server writes the group of the synced block a pointer names each time the page is saved, and the admin never shows it. unwrapSyncedBlocks reads every translation in the page’s locale in one request, even when the pointer names a translation the website’s key can’t read, such as an unpublished one:

          const { data: page } = await cms.get("page", id);
const blocks = await unwrapSyncedBlocks(cms, page.blocks, { locale: "fr" });
        

A pointer saved before pointers stored their group costs a second request that looks the groups up, and that lookup only finds synced blocks the key can read. Save the page again to store it.

In the admin

The Synced blocks collection lists each one’s block type and whether it’s published. Used in, in a synced block’s menu, lists the pages placing it, with links.

Deleting a synced block doesn’t offer to replace it with another, and ?replaceWith= is refused: a replacement rewrites the pages without the checks above, so it could place a type a page can’t hold. Point the pages at another synced block first, then delete it.

Moving an existing synced block entity onto the plugin

A project with its own synced_block entity and pointer, shaped as above (a title, a block union, and a pointer object("synced_block", { fields: [relation("source", ...)] })), moves without a data migration: the plugin’s entity and pointer store the same columns and JSON.

  1. Remove your synced_block entity from entities and register syncedBlocks({ blocks, translatable }) with the blocks its block union held. Keep its name, or pass it as entity.
  2. Replace your pointer block with synced.block() in each union.
  3. Remove your own placement checks from the pages’ hooks, and pass the synced block’s other hooks (index refreshes, notifications) as hooks. The plugin’s entity has only title and block, so a before-hook that writes another field (a slug) fails every save: drop it, or keep that entity as your own.
  4. Replace your website’s unwrap with unwrapSyncedBlocks.
  5. Run cms migration generate --check: it should find nothing to change.

Previous

Shopify

Next

REST API