Block library: noSnippet option for sections repeated on every page

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-10-29 14:13:37 +01:00
co-authored by Claude Opus 5.5
parent 2a07fb8844
commit 6e6de5d0cc
4 changed files with 20 additions and 3 deletions
+12 -2
View File
@@ -8,6 +8,7 @@ import type { PartClasses } from './ui/cx.js'
import type { MediaProps } from './ui/Media.js'
import type { SectionProps } from './ui/Section.js'
import { NoSnippet } from '../seo/NoSnippet.js'
import { ContactBlock } from './blocks/contact/Component.js'
import { CtaBlock } from './blocks/cta/Component.js'
import { EntryListBlock } from './blocks/entryList/Component.js'
@@ -59,6 +60,11 @@ export type BlockRegistryOptions = {
components?: Partial<Record<LibraryBlockSlug, ComponentType<never>>>
/** The project's own blocks, in the same registry. */
extra?: BlockComponentMap
/**
* Blocks kept out of Google's snippets (`data-nosnippet`) — for sections repeated on every page,
* e.g. `['cta']`, so the same call to action is not quoted under every result. Default none.
*/
noSnippet?: readonly LibraryBlockSlug[]
/** Turn on only these library blocks. Default: all 15 (an unused entry costs nothing). */
only?: readonly LibraryBlockSlug[]
/** Renamed slugs (BlockOptions `slug`), so the registry key matches the stored blockType. */
@@ -77,13 +83,17 @@ export type BlockRegistryOptions = {
* Everything is bound on the server, so no function crosses to the client.
*/
export function createBlockRegistry(content: BlockContent, options: BlockRegistryOptions = {}): BlockComponentMap {
const { backgrounds, classNames = {}, components = {}, extra = {}, only, slugs = {}, ui: own = {}, ...rest } = options
const { backgrounds, classNames = {}, components = {}, extra = {}, noSnippet = [], only, slugs = {}, ui: own = {}, ...rest } = options
const ctx: BlockContext = { ...rest, content, lightbox: rest.lightbox ?? true }
const ui = buildUi(ctx, own, backgrounds)
const map: BlockComponentMap = {}
for (const slug of only ?? (Object.keys(LIBRARY_BLOCK_COMPONENTS) as LibraryBlockSlug[])) {
const Block = (components[slug] ?? LIBRARY_BLOCK_COMPONENTS[slug]) as ComponentType<Record<string, unknown>>
const bound = (props: Record<string, unknown>) => <Block {...props} classNames={classNames[slug]} ctx={ctx} ui={ui} />
const hidden = noSnippet.includes(slug)
const bound = (props: Record<string, unknown>) => {
const block = <Block {...props} classNames={classNames[slug]} ctx={ctx} ui={ui} />
return hidden ? <NoSnippet>{block}</NoSnippet> : block
}
bound.displayName = `Library(${slug})`
map[slugs[slug] ?? slug] = bound
}