diff --git a/.changeset/block-renderers-module.md b/.changeset/block-renderers-module.md new file mode 100644 index 00000000..1ed76770 --- /dev/null +++ b/.changeset/block-renderers-module.md @@ -0,0 +1,28 @@ +--- +'@tanstack/cms-core': minor +'@tanstack/cms-ui-react': minor +--- + +**Breaking:** block renderers move out of `blockTypes`. `BlockType.component` +is removed, so a block type is data only (`type`, `label`, `fields`) and the +config never imports React. `defineBlock`, `defineCms`, and plugins throw when a +block type still has a `component`, naming where it goes. + +Export the components from a renderers module with `defineRenderers()` (from +`@tanstack/cms-ui-react` or `/site`) and name its path, relative to the routes +directory, in `createRoutes(definition, { renderers })`. The CMS's own routes +(public pages, preview, on-page editor) render blocks with it; the +`tanstackCms()` Vite plugin hands it to them, so `vite.config.ts` passes a path +and never loads React. In your own routes, pass the same object to +`BlocksView`, `BlockView`, or `InteractiveDocument` as the new `renderers` prop. +A block's renderer is the prop's, then the module's, then a built-in, then the +generic field view; an app renderer can replace a built-in. + +```tsx +// src/cms.renderers.tsx +import { defineRenderers } from '@tanstack/cms-ui-react/site' +export const renderers = defineRenderers({ blocks: { quote: Quote } }) + +// vite.config.ts +createRoutes(definition, { renderers: '../cms.renderers.tsx' }) +``` diff --git a/.claude/skills/astro-starlight-to-tanstack-cms/SKILL.md b/.claude/skills/astro-starlight-to-tanstack-cms/SKILL.md index 87a67d05..0fd5480f 100644 --- a/.claude/skills/astro-starlight-to-tanstack-cms/SKILL.md +++ b/.claude/skills/astro-starlight-to-tanstack-cms/SKILL.md @@ -129,8 +129,9 @@ a `hero` block; translations can become `locale`/`translationOf` documents). Block palette: a docs migration only needs `richText` (`markdown` field type). Add block types only if you decompose bodies into something richer than one Markdown blob; the renderer has built-in `callout`, `code`, -`columns`, `embed`, `image`, `cta`, and `hero` components, and -`defineBlock({ component })` takes a custom one. +`columns`, `embed`, `image`, `cta`, and `hero` components, and the app's +renderers module (`defineRenderers({ blocks })`, named by +`createRoutes(definition, { renderers })`) adds a custom one. ## 4. Migrate the data @@ -204,7 +205,7 @@ Always consider: - **MDX components** — every component flattened by the migrator (it prints the list). Each needs a block type + renderer to restore its behavior - (`defineBlock({ component })`), or stays text-only. + (`defineBlock` plus a `defineRenderers({ blocks })` entry), or stays text-only. - **Explicit sidebars** — the docs layout derives groups from slug segments; hand-written `sidebar` entries (external links, custom group labels, `sidebar.badge`, `sidebar.hidden`) need app work. diff --git a/.claude/skills/emdash-to-tanstack-cms/SKILL.md b/.claude/skills/emdash-to-tanstack-cms/SKILL.md index 78d01052..0b9279f9 100644 --- a/.claude/skills/emdash-to-tanstack-cms/SKILL.md +++ b/.claude/skills/emdash-to-tanstack-cms/SKILL.md @@ -157,7 +157,8 @@ For a migrated post that's `imageField: 'hero'`, `summaryField: 'excerpt'`, The renderer has built-in components for `hero`, `richText`, `image`, `cta`, `callout`, `code`, `columns`, and `embed`; other block types use the generic -renderer or a `defineBlock({ component })`. +renderer or a custom one in the app's renderers module +(`defineRenderers({ blocks })`, named by `createRoutes(definition, { renderers })`). ## 4. Migrate the data diff --git a/.claude/skills/payload-to-tanstack-cms/SKILL.md b/.claude/skills/payload-to-tanstack-cms/SKILL.md index c6b82c68..c1937209 100644 --- a/.claude/skills/payload-to-tanstack-cms/SKILL.md +++ b/.claude/skills/payload-to-tanstack-cms/SKILL.md @@ -202,8 +202,9 @@ with fields mapped by the same table. A block's Lexical `content` usually becomes the `richText` block's `markdown` field. The renderer in `@tanstack/cms-ui-react` has built-in components for `hero`, `richText`, `image`, `cta`, `callout`, `code`, `columns`, and `embed`; any other block -type renders with the generic field renderer, or pass a `component` in -`defineBlock` for a custom one. +type renders with the generic field renderer, or add a custom one to the +app's renderers module (`defineRenderers({ blocks })`, named by +`createRoutes(definition, { renderers })`). ## 4. Migrate the data diff --git a/.claude/skills/wordpress-to-tanstack-cms/SKILL.md b/.claude/skills/wordpress-to-tanstack-cms/SKILL.md index d89b1deb..5cd2bf42 100644 --- a/.claude/skills/wordpress-to-tanstack-cms/SKILL.md +++ b/.claude/skills/wordpress-to-tanstack-cms/SKILL.md @@ -131,7 +131,9 @@ SEO tags (`cmsHead`), the home page's post cards, and the on-page editor read. Post body block palette: `richText` (`markdown` field type) covers converted bodies. The renderer also has built-in `hero`, `image`, `cta`, `callout`, `code`, `columns`, and `embed` components; use `htmlToBlocks` (§5) to emit -them, or `defineBlock({ component })` for custom Gutenberg blocks. +them, or a `defineBlock` plus a renderer in the app's renderers module +(`defineRenderers({ blocks })`, named by `createRoutes(definition, { renderers })`) +for custom Gutenberg blocks. ## 4. Migrate the data diff --git a/docs/api/cms-core.md b/docs/api/cms-core.md index 074dd6ae..40e705ab 100644 --- a/docs/api/cms-core.md +++ b/docs/api/cms-core.md @@ -683,7 +683,7 @@ function defineCollection(collection: TCol function defineBlock(block: TBlock): TBlock ``` -Identity helpers that keep literal types for `InferDoc` / `InferBlock`. +Identity helpers that keep literal types for `InferDoc` / `InferBlock`. `defineBlock` throws when the block type has a `component`: block renderers live in the app's renderers module (see [Custom block renderers](../schema/blocks-and-rendering#custom-block-renderers)). ### `collections()`, `blockTypes()`, `blockTypeMap()` @@ -773,7 +773,7 @@ function isFieldVisible(field: Field, siblings: Record; defaultSort?: { field: string; dir?: 'asc' \| 'desc' } }`. | | `CollectionHooks` | Pre-commit lifecycle hooks: `preValidate`, `postValidate`, `preDelete`, plus the `postRead` response transform. `afterChange` / `afterDelete` are deprecated in favour of `defineEventHandler`. See [Events & Hooks](../architecture/events-and-hooks). | | `ChangeHookArgs` | `{ doc, id, operation, actor }` — what a pre-commit mutating hook receives. | -| `BlockType` | `{ type, label, fields, component? }`. | +| `BlockType` | `{ type, label, fields }`. Data only: renderers are bound with `defineRenderers` from `@tanstack/cms-ui-react`, and a block type with a `component` throws. | | `Block` | `{ _type: string } & { [key: string]: JsonValue }`. | | `Doc` | `{ [key: string]: JsonValue }`. | | `JsonValue` | Any JSON value. | diff --git a/docs/api/cms-ui-react.md b/docs/api/cms-ui-react.md index ed92f086..5afa77bc 100644 --- a/docs/api/cms-ui-react.md +++ b/docs/api/cms-ui-react.md @@ -51,10 +51,11 @@ interface BlocksViewProps { blocks: ReadonlyArray className?: string // default 'space-y-8' onSelectBlockField?: (blockIndex: number, fieldName: string) => void + renderers?: CmsRenderers } ``` -Renders an ordered array of blocks with your `defineCms({ blockTypes })` components, falling back to the built-in renderers. See [Blocks & Rendering](../schema/blocks-and-rendering). +Renders an ordered array of blocks. Each block's renderer is `renderers.blocks[_type]` from the prop, then from the renderers module bound through `createRoutes(definition, { renderers })`, then a built-in renderer, then a generic view of its fields. See [Blocks & Rendering](../schema/blocks-and-rendering#custom-block-renderers). ### `BlockView` @@ -64,11 +65,29 @@ function BlockView(props: BlockViewProps): JSX.Element interface BlockViewProps { block: Block onSelectField?: (fieldName: string) => void + renderers?: CmsRenderers } ``` Renders a single block. +### `defineRenderers(renderers)` + +```ts +function defineRenderers(renderers: CmsRenderers): CmsRenderers + +interface CmsRenderers { + blocks?: { readonly [type: string]: ComponentType } +} + +interface BlockRendererProps { + block: Block + onSelectField?: (fieldName: string) => void +} +``` + +Declares the app's React renderers, keyed by block `_type`; exported from both the package root and `/site`. It checks the shape and returns the object. Export the result as `renderers` from a module (e.g. `src/cms.renderers.tsx`), name that module's path in `createRoutes(definition, { renderers })` for the CMS's own routes, and pass the object as the `renderers` prop in your own. Block types stay data only: `defineBlock`, `defineCms`, and plugins reject a block type with a `component`. + ```tsx validate=type import { BlocksView } from '@tanstack/cms-ui-react/site' import type { Block } from '@tanstack/cms-core' @@ -372,6 +391,7 @@ interface CreateRoutesSpec { api?: CmsRouteSurfaceSpec<'api'> custom?: ReadonlyArray rootFile?: string + renderers?: string // the app's renderers module, relative to the routes directory } // true: every route; false: none; a key list: only those; @@ -390,7 +410,7 @@ type CmsRouteKey = type CmsSurfaceRouteKey // the keys of one surface ``` -The virtual route tree for the TanStack Router plugin: the public pages, the admin, and the API routes, plus your own `custom` routes. The admin base path and plugin routes come from the definition. A surface you leave out is mounted, so `createRoutes(definition)` mounts everything. Route keys are stable public API: renaming or removing one is a breaking change. Keys, paths, and options are listed in the [route table](./routes-and-endpoints#route-table); overriding and adding routes is covered in [Building Pages](../schema/building-pages). +The virtual route tree for the TanStack Router plugin: the public pages, the admin, and the API routes, plus your own `custom` routes. The admin base path and plugin routes come from the definition. A surface you leave out is mounted, so `createRoutes(definition)` mounts everything. `renderers` is the path of a module that exports `renderers = defineRenderers({ … })`; the CMS routes render blocks with it. It is a path rather than an import so `vite.config.ts` never loads React, and it must exist when the config loads. Route keys are stable public API: renaming or removing one is a breaking change. Keys, paths, and options are listed in the [route table](./routes-and-endpoints#route-table); overriding and adding routes is covered in [Building Pages](../schema/building-pages). --- @@ -410,10 +430,16 @@ interface CmsVitePlugin { ssr: { noExternal: Array } optimizeDeps: { exclude: Array } } + resolveId: ( + id: string, + importer: string | undefined, + options?: { ssr?: boolean }, + ) => string | undefined + load: (id: string) => string | undefined } ``` -The CMS packages ship TanStack Start server functions and route files that your app's Start compiler must transform. The plugin keeps `@tanstack/cms-core`, `@tanstack/cms-api`, `@tanstack/cms-ui-react`, and `@tanstack/cms-client` (plus any `packages` you add) out of Vite's SSR externals and dependency pre-bundling, and bundles `@tanstack/ai*` into SSR alongside them. `routesDirectory` and `generatedDirectory` are read by `createRoutes()`, which resolves route files when the config loads. Add it before `tanstackStart()`. +The CMS packages ship TanStack Start server functions and route files that your app's Start compiler must transform. The plugin keeps `@tanstack/cms-core`, `@tanstack/cms-api`, `@tanstack/cms-ui-react`, and `@tanstack/cms-client` (plus any `packages` you add) out of Vite's SSR externals and dependency pre-bundling, and bundles `@tanstack/ai*` into SSR alongside them. In SSR, `resolveId` and `load` serve `use-sync-external-store` as ESM on React's own `useSyncExternalStore`, so a production server bundle under pnpm loads one React. `routesDirectory` and `generatedDirectory` are read by `createRoutes()`, which resolves route files when the config loads. When `createRoutes(definition, { renderers })` names a renderers module, the plugin's `load` hook serves the package's `app-renderers` module (which every CMS page route imports) as code that binds it. Add it before `tanstackStart()`. ```ts validate=type import { defineConfig } from 'vite' diff --git a/docs/api/routes-and-endpoints.md b/docs/api/routes-and-endpoints.md index 53db57b3..df7c0005 100644 --- a/docs/api/routes-and-endpoints.md +++ b/docs/api/routes-and-endpoints.md @@ -60,6 +60,7 @@ Admin paths move under the definition's `admin.basePath` when one is set (for ex | `api` | surface spec | `true` | The API routes. | | `custom` | `Array` | none | Your app's own routes, built with `route()` / `index()` / `physical()` from `@tanstack/virtual-file-routes`, attached after the CMS and plugin routes. | | `rootFile` | `string` | package `__root.tsx` | Your own root route file. | +| `renderers` | `string` | none | Your renderers module (`export const renderers = defineRenderers({ blocks })`), relative to the routes directory. The CMS routes render blocks with it. See [Custom block renderers](../schema/blocks-and-rendering#custom-block-renderers). | Each surface takes the same values: diff --git a/docs/architecture/routes-in-a-package.md b/docs/architecture/routes-in-a-package.md index 06fb0bc8..8991e532 100644 --- a/docs/architecture/routes-in-a-package.md +++ b/docs/architecture/routes-in-a-package.md @@ -102,6 +102,7 @@ The spec says what to mount; with no spec, everything is mounted. | `public`, `admin`, `api` | Per surface: `true` or left out (every route), `false` (none), `['review']` (only these keys), or `{ agent: false, edit: './edit.tsx' }` (drop or replace single keys) | | `custom` | Your app's own routes (`route()`, `index()`, `physical()`), attached after the CMS and plugin routes | | `rootFile` | Your own root route instead of the package's `__root.tsx` | +| `renderers` | Path of your renderers module (`export const renderers = defineRenderers({ blocks })`); the CMS routes render blocks with it. See [Custom block renderers](../schema/blocks-and-rendering#custom-block-renderers) | The admin base path and plugin routes aren't options: they come from the definition. The build directories are options of `tanstackCms()`: diff --git a/docs/deployment/upgrading.md b/docs/deployment/upgrading.md index c94bb8ed..f9395f19 100644 --- a/docs/deployment/upgrading.md +++ b/docs/deployment/upgrading.md @@ -312,6 +312,27 @@ createRoutes(definition, { `tanstackCms()` has to come before `tanstackStart()` in `plugins`, because `createRoutes()` reads its `routesDirectory` and `generatedDirectory` when Start loads the router config. The route keys (`CmsRouteKey`, and `CMS_ROUTE_KEYS` from `@tanstack/cms-core/definition`) are stable public API. +### 22. Block renderers move out of `blockTypes` + +`BlockType.component` is removed, so block types are data only and the config never imports React. `defineBlock`, `defineCms`, and plugins throw when a block type still has a `component`. Move each component into a renderers module and name its path in `createRoutes`; the CMS's own routes (public pages, preview, on-page editor) render with it. In your own routes, pass the same object to `BlocksView`, `BlockView`, or `InteractiveDocument` as `renderers`. + +```tsx +// before: src/cms.config.ts +defineBlock({ type: 'quote', label: 'Quote', fields, component: Quote }) + +// after: src/cms.config.ts +defineBlock({ type: 'quote', label: 'Quote', fields }) + +// after: src/cms.renderers.tsx +import { defineRenderers } from '@tanstack/cms-ui-react/site' +export const renderers = defineRenderers({ blocks: { quote: Quote } }) + +// after: vite.config.ts (a path, relative to src/routes) +createRoutes(definition, { renderers: '../cms.renderers.tsx' }) +``` + +There's no codemod: the components usually sit inline in the config and need their imports moved with them. The built-in renderers (`hero`, `richText`, …) are unchanged, and a `renderers.blocks` entry with the same `_type` replaces one. See [Custom block renderers](../schema/blocks-and-rendering#custom-block-renderers). + ## Versioning policy From 1.0, TanStack CMS follows [semantic versioning](https://semver.org). The diff --git a/docs/migration/from-astro-starlight.md b/docs/migration/from-astro-starlight.md index 43043fb0..fd969f21 100644 --- a/docs/migration/from-astro-starlight.md +++ b/docs/migration/from-astro-starlight.md @@ -26,7 +26,7 @@ Where the [Payload migration](./from-payload) converts Lexical JSON, Starlight c | **Runtime** | Build-time static site; every edit is a commit and a rebuild | A TanStack Start server; edits publish immediately, or through the review queue | | **Storage** | `.md`/`.mdx` files in git | A `StorageAdapter` (`createSqliteStorage()` by default, `createLibsqlStorage()` for Turso), with a revision per commit | | **Schema** | Content collections with `docsSchema()` frontmatter | `defineCms({ collections, blockTypes })` with `theme: { variant: 'docs' }` | -| **Components** | MDX with compile-time JSX (``, `