/sitecore-content-sdk-nextjs-pages-router:content-sdk-pages-router-editing-safe-renderingfrom Sitecore Content SDK - Next.js Pages Router
Ensures Content SDK Pages Router components render correctly in SitecoreAI editing, preview, and design library contexts. Guards on context.preview in getStaticProps/getServerSideProps and uses isDesignLibraryPreviewData to route between getDesignLibraryData and getPreview.
Ships in Sitecore Content SDK - Next.js Pages Router. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Ensure components behave correctly in SitecoreAI editing, preview, and design library. This app uses context.preview and context.previewData in getStaticProps/getServerSideProps for editing data.
<label> jumps focus into its input instead of selecting the field. See "Authoring-safe components" below.src/pages/[[...path]].tsx), use context.preview and context.previewData. When in preview, use isDesignLibraryPreviewData(context.previewData) to distinguish: if true, use client.getDesignLibraryData(context.previewData); otherwise use client.getPreview(context.previewData). When not in preview, use getPage(path, { locale: context.locale }) then getDictionary and getComponentData as usual.src/pages/api/editing/config.ts uses EditingConfigMiddleware({ components, metadata }).getHandler() (import components from .sitecore/component-map, metadata from .sitecore/metadata.json). src/pages/api/editing/render.ts uses EditingRenderMiddleware().getHandler(). src/pages/api/editing/feaas/render.ts uses FEAASRenderMiddleware().getHandler(). Export the handler as default. Do not duplicate client creation; config uses the same component map as the app.<label htmlFor> in edit. These break authoring in Pages but pass unit tests — verify in a real editor.The plumbing above gets editing data into the page; this section keeps each component authorable once it's there. Symptom class: the page renders fine but an author can't select/manage a field in Pages. Applies to field-driven components (built with Text/RichText/Image/Link), especially anything interactive (forms, labelled inputs) or with optional fields.
Detect edit mode inside a component:
const { page } = useSitecore(); const isEditing = page.mode.isEditing; (useSitecore from @sitecore-content-sdk/nextjs; needs SitecoreProvider in the tree — the scaffold provides it). Pages Router has no React Server Components, so every component may use the hook. useSitecore() is the unified hook; there is no useSitecoreContext in Content SDK.field.metadata is present — const isEditing = !!fields?.SomeTextField?.metadata;. Sitecore emits field.metadata only in edit/preview. TextField/RichTextField extends FieldMetadata; ImageField/LinkField carry metadata only via & FieldMetadata, so read editing off a text field. Useful for shared code that also targets App Router server components.1. Empty fields must stay authorable — gate on existence + edit mode, never on value. {fields?.Cta?.value?.href && <Link .../>} hides the field in the editor too, so there's nothing to click. The SDK helpers already render the default empty-field editing placeholder when field.metadata is present + value empty + editable !== false, and render null when empty + published — they just have to be rendered:
{fields?.Cta && (isEditing || fields.Cta.value?.href) && (
<Link field={fields.Cta} className="btn" />
)}
The existence check (fields?.Cta &&) stops an absent datasource field (e.g. a template you haven't finished building) from feeding undefined to the helper and crashing it. For repeated/flat fields (e.g. MetaValue1-3/MetaLabel1-3), render all rows in editing and .filter() to non-empty only when published.
2. Presentational forms must go inert in edit mode. A <form onSubmit={validate}> with a type="submit" button whose label is a field: clicking the button to author the label fires submit → validation errors + focus-stealing, so the field can't be managed.
function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
if (isEditing) return; // no validation in the editor
/* ...validate / set state... */
}
// submit button:
<button type={isEditing ? 'button' : 'submit'}>…</button> // also neutralizes Enter-to-submit
3. Field-backed <label htmlFor> steals the click. A <label htmlFor="x"><Text field={NameLabel}/></label> focuses its associated input on click (native label behavior) instead of selecting the label field. When the label is a sibling of its input (associated only via htmlFor), drop the association in edit:
<label htmlFor={isEditing ? undefined : 'x'} className="field-label">
<Text field={fields?.NameLabel} />
</label>
Published keeps the proper label → input a11y association (isEditing is false in normal mode).
4. Custom / transformed field presentation must stay editing-safe. If a component renders a field's raw field.value with its own markup — splitting a headline into animated spans, formatting a number, concatenating a string — instead of passing the field to <Text>/<RichText>, it never emits the editing chrome (field.metadata is dropped), so the field is not inline-editable in Pages (only the right-hand Content panel can change it), while sibling fields rendered through the SDK helpers stay inline-editable. This is the "some fields inline-edit, some don't" tell. Branch on edit mode — SDK field component when editing, the custom presentation when published:
export function KineticHeadline({ field }: { field: TextField }) {
const isEditing = !!field?.metadata; // router-agnostic edit signal
if (isEditing) return <Text field={field} tag="h1" />; // ONE editable node, un-split
return <h1 aria-label={String(field?.value ?? '')}>{/* split .kin-line spans */}</h1>;
}
Keep it one editable node in edit mode — never split one field across multiple <Text> (each would try to save the whole value → corruption). Preserve a11y in both branches (accessible name via aria-label; decorative pieces aria-hidden).
5. Integrated GraphQL (ComponentQuery) text fields must be fetched as { value jsonValue } — check the DATA path FIRST, not just the render. For a Shape-B / ComponentQuery component (fields.data.datasource…), a text field fetched as field(name:"X") { value } returns the raw string with no editing metadata, so it is not inline-editable no matter how you render it — <Text>, ed(), everything is downstream of a field that has no .metadata. This is the deeper cause behind "I already render through <Text> and it still won't edit": the render was never the blocker; the query was. Fix on the rendering item's ComponentQuery: request { value jsonValue } (the universal safe shape — .value keeps rendering working, .jsonValue carries { value, editable, metadata }), then render the metadata-bearing jsonValue:
type Cq<F> = F & { jsonValue?: F };
const ed = <F,>(f?: Cq<F>): F | undefined => f?.jsonValue ?? f; // defensive: falls back to value-only
// ComponentQuery: Title: field(name:"Title") { value jsonValue }
<Text field={ed(item.Title)} /> // reads the editable jsonValue; still renders if jsonValue absent
Image/Link fields already use { jsonValue } (already editable). When a Shape-B field won't edit, read the LIVE ComponentQuery first — a { value } text selection is the smoking gun; don't just inspect the component. (See content-sdk-pages-router-graphql-data-fetching for the query; the value-vs-jsonValue asymmetry also affects rendering.)
Verify in a real editor, not jest. All five only surface in Pages (they are metadata-driven). A structural unit test that reads props directly passes regardless — Playwright/manual editor smoke is the gate.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
11 skills in this plugin
/content-sdk-pages-router-component-data-strategyComponent data in a Content SDK Pages Router app: getComponentData after getPage resolves component props for the layout. Use when wiring component data or BYOC in a Pages Router app. Not App Router./content-sdk-pages-router-component-registrationRegisters components in the generated .sitecore/component-map.ts of a Content SDK Pages Router app (regenerate, never hand-edit). Use when adding a component or layout or the editor cannot find one. Not App Router./content-sdk-pages-router-component-scaffoldCreates a new Sitecore component in a Content SDK Pages Router app: file layout, props interface, src/components/ placement, generated component map. Use when adding or scaffolding a component. Not App Router./content-sdk-pages-router-component-variantsImplements component variants: different renderings or data-driven variants of the same component type. Pages Router: register in .sitecore/component-map.ts; getComponentData resolves props. Use when one component has multiple presentations./content-sdk-pages-router-dictionary-and-i18nDictionary and i18n in a Content SDK Pages Router app: next.config i18n locales, context.locale, getDictionary after getPage. Use when adding translated content or changing locale behaviour. Not App Router./content-sdk-pages-router-graphql-data-fetchingFetches page, dictionary and component data in a Content SDK Pages Router app: getPage, getDictionary, getComponentData, getPagePaths for SSG. Use when fetching page or dictionary content. Not App Router.npx claudepluginhub chris1415/sitecore-skills-pocs --plugin sitecore-content-sdk-nextjs-pages-routerOther plugins' skills for the same job