By ··10 min read

Sitecore Pages editor hydration errors in Next.js: two causes

Components became unselectable in the Pages editor and React logged hydration errors. Here is what hydration is, how to read the diff, and the two separate causes I found.

On a Sitecore Content SDK (Software Development Kit) site, a component stopped responding in Pages, Sitecore’s visual page editor. Clicking it selected nothing, the right-hand panel kept saying “Select an element in the canvas”, and the Next.js overlay showed this:

Hydration failed because the server rendered HTML didn't match the client.
As a result this tree will be regenerated on the client.

The page rendered fine. The component just could not be edited. Two unrelated things were producing the same message, and each needed its own fix.

The stack was Content SDK 1.0, Next.js 15.5 (Pages Router) and React 19. Newer SDK releases have since fixed part of the second cause, and I note where below.

What hydration is

Next.js renders each page to HTML on the server so the browser has something to show straight away. Then React loads in the browser and hydrates that HTML: it renders the same components again, in memory, and attaches itself to the HTML that is already there instead of building it from scratch. That is much cheaper than starting over, and it keeps the page from flashing.

The shortcut only works if the two renders agree. React walks the existing HTML and the tree it just built, side by side. If an element, a class or a piece of text differs, it cannot trust the HTML, so it throws it away and renders again on the client. That is the “regenerated on the client” in the error.

The server sees isEditorActive false and sends a fixed div; the browser sees true and builds a relative div; React reports a mismatch
The same component, rendered twice, with different answers.

In the Pages editor this costs more than speed. Sitecore adds editing markup to the page: <code> elements that describe each field, and hooks the inline editor attaches to. When React regenerates the tree, that markup is lost, and the component can no longer be selected. A hydration error here means “this component is not editable”.

Read the diff first

The overlay shows a diff. React marks what the browser rendered with + and what was already in the page with -. The kind of difference tells you which cause you have.

Cause 1: differences your code chose
<div
+ id="header-container"
- id={null}
+ className={"relative top-0 left-0 w-full z-50 transition-transform ..."}
- className="bg-Black"
- style={{height:"103px"}}
>
Cause 2: differences the editor made
<Text field={{metadata:{...}, ...}}>
<FieldMetadata metadata={{...}}>
<code>
+ Stay Connected
- <span>

The first is an id, a className and a style that depend on a condition in my component: the server put a spacer div where the browser put the header. The second has no condition to blame: the browser rendered a bare piece of text, and by the time React looked, the page held a <span>. Signs of the second kind are contenteditable, ck-content classes, [No text in field] and cursor: pointer that nothing in your code sets.

I misread this at first. I added suppressHydrationWarning to several elements, moved it up to the component root when that did nothing, and then found out why it could not work. That prop only silences a text or attribute difference on the element it sits on, one level deep. It does nothing for a different element or a different number of children.

Cause 1: a flag that is not the same on both sides

The Content SDK exports isEditorActive(). Its source is short:

@sitecore-content-sdk/core, editing/utils.js
static isActive() {
if (isServer()) {
return false;
}
const chromesCheck = window.location.search.indexOf('sc_headless_mode=edit') > -1;
return chromesCheck || !!window.document.getElementById(PAGES_EDITING_MARKER);
}

On the server it is always false, because there is no window to ask. In the editor’s browser it becomes true. Any component that uses it to choose what to render gives the server one tree and the browser another.

src/components/SiteHeader.tsx
import { isEditorActive } from '@sitecore-content-sdk/nextjs/utils';
// ...
{!isEditorActive() && <div style={{ height: headerHeight }} className="bg-Black" />}
<div
className={`${isEditorActive() ? 'relative' : 'fixed'} transition-transform`}

The editing flag the SDK passes down through React context is page.mode.isEditing. It is true in edit mode on the server and in the browser, so both renders agree.

src/components/SiteHeader.tsx
import { useSitecore } from '@sitecore-content-sdk/nextjs';
const { page } = useSitecore();
const isEditing = page.mode.isEditing;
// ...
{!isEditing && <div style={{ height: headerHeight }} className="bg-Black" />}
<div
className={`${isEditing ? 'relative' : 'fixed'} transition-transform`}

I found the header first. After fixing it the overlay count went from six issues to five and the next error moved down the page, to the main placeholder. A search showed about 70 files using the SDK function. Each one that used it in render was a mismatch waiting for someone to open that page.

Sweeping the call sites

To keep the change reviewable I added one shared function with the same name, so most files needed only a new import.

src/lib/editing.ts
import { useSitecore } from '@sitecore-content-sdk/nextjs';
// Same on the server and in the browser, unlike the SDK's isEditorActive().
// It is a hook: call it unconditionally in render.
export const isEditorActive = (): boolean => {
const { page } = useSitecore();
return Boolean(page?.mode?.isEditing);
};

That is where the rules of hooks bite. A hook cannot run inside an event handler, a useEffect callback or a .map() callback. Files that used the flag only in render just swapped the import. Files that also used it in an effect or a handler read the flag once at the top of the component and used that constant everywhere.

src/components/NewsletterSignup.tsx
const { page } = useSitecore();
const isEditing = page.mode.isEditing; // read once, in render
// ...
const onSubmit = () => {
if (!email || isEditorActive()) return;
if (!email || isEditing) return;

A call that sits only in a handler or an effect is safe as it was, since no server render is compared there. Converting those too was a choice for consistency, not a fix.

My bulk replace also converted calls in helper functions that had no such constant in scope. The TypeScript compiler (tsc) caught it. A sweep like this needs a type check after every batch.

The sweep did not survive as one change. We later reverted it to test the other cause in isolation, and chose to fix each component when its error shows up in the editor.

Not every condition is about the flag

One component still failed after the swap, because its conditions did more than read the flag. They swapped <Text> for a plain <h3>, and returned null for an empty field in one pass and not the other. Changing which element renders, or whether one renders at all, is the worst kind of mismatch.

The fix was to always render the SDK field component and let it decide what to show. <Text> prints the value on the live site and an editable field in the editor, and <RichText> prints nothing for an empty field live and a placeholder in the editor. Where a rule hides something, I hide it with a CSS class on an element that is always mounted.

Cause 2: markup the editor changes

With the flag fixed in the components on that page, errors remained that could not be traced to a condition in my code. These are the ones in the second diff above.

A <Text> with no tag. In SDK 1.0, Text without a tag returns a React fragment holding a bare text node, in edit mode too. Neither my code nor the SDK rendered that <span>, so the inline editor added it, most likely because it needs an element to hold contenteditable. A text node turning into an element is a structural change, and no suppress prop covers that. RichText defaults to a div, so it was immune.

src/components/NewsletterBlock.tsx
<Text field={props.fields.Title} />
<Text field={props.fields.Title} tag="span" />

From SDK 1.4 (I checked 1.4, 1.5 and 1.6), Text renders a <span> with suppressHydrationWarning whenever the field carries editing metadata, so newer projects should not need this change.

An empty field’s placeholder. An empty <Text> or <RichText> renders the SDK’s <span>[No text in field]</span>, and the editor then decorates that span with classes and attributes. SDK 1.0 renders that placeholder without passing it any props, so I could not put suppressHydrationWarning on it. A field accepts an emptyFieldEditingComponent, so I passed my own span that carries the prop.

src/components/content-sdk/EmptyFieldEditingPlaceholder.tsx
import React from 'react';
export const EmptyFieldEditingPlaceholder = (): React.JSX.Element => (
<span suppressHydrationWarning>[No text in field]</span>
);

SDK 1.6 puts suppressHydrationWarning on its own empty-field placeholder and passes it to a custom one, so this workaround is also only for older versions.

Suspense boundaries. React’s Suspense component marks a part of the tree that can show a fallback while it loads, and React also uses it as the unit it re-renders when it recovers from a hydration error. The SDK wraps each component it renders from a placeholder in an error boundary and a Suspense boundary. When a mismatch happens inside a boundary, React re-renders that boundary on the client, which is exactly what destroys the editing markup. The Placeholder has a disableSuspense prop that removes the boundary, read by the error boundary.

Two details made it harder than one prop. It reads only the placeholder’s own prop, with no global default, and a placeholder nested inside a child component does not get it unless that component passes it on. So I wrapped the SDK placeholder once and used the wrapper in the layout and in every container component.

src/components/content-sdk/EditingPlaceholder.tsx
import { ComponentProps, JSX } from 'react';
import { Placeholder, useSitecore } from '@sitecore-content-sdk/nextjs';
// disableSuspense is not inherited by nested placeholders, so every placeholder uses this.
export const EditingPlaceholder = (props: ComponentProps<typeof Placeholder>): JSX.Element => {
const { page } = useSitecore();
return <Placeholder {...props} disableSuspense={props.disableSuspense ?? page.mode.isEditing} />;
};

It only turns Suspense off while editing, so the published site is unchanged. Sitecore’s older JSS (JavaScript Services) SDK made the same change in version 22.10, turning Suspense off in placeholders while editing. In Content SDK 2.0, disableSuspense is true by default.

What my test did and did not show

The tag change and the custom component each cleared one error at a time, and I had to find each component first. disableSuspense looked like it could clear them together. To test it, we reverted everything, including the Cause 1 swaps, and added only EditingPlaceholder.

The overlay then showed one error instead of several, and it was a Cause 1 error: the header. I fixed the header and stopped there, so I did not confirm that the page reached zero errors.

That one error is weaker evidence than it looks. Without a Suspense boundary to stop at, React regenerates the whole tree at the first mismatch it meets, so it may never report the mismatches further down the page. That is my reading of how React recovers, not something I measured. Treat disableSuspense as a promising fix that needs a clean run on your own pages.

Cause 1, a client-only flag in render, is fixed with page.mode.isEditing. Cause 2, markup the editor changes, was worked around on SDK 1.0 with a tag prop or by turning off Suspense while editing.
Match the diff to the cause, then to the fix.

Does static generation change this?

I wondered if our move to static generation (SSG, pages built ahead of time) was relevant. It was not. SSG and server rendering both send HTML that React then hydrates, so the comparison happens either way. Editing runs through Next.js Preview Mode, which renders each request on the server with the editing markup in place. Caching is where static pages bite instead: see why a cached notFound page stays a 404 after a publish.

How to check it worked

  1. Run the app locally (npm run dev), open a page in the Pages editor, and look at the Next.js badge in the corner. The aim is no issues, with no error overlay.
  2. Click a component with an empty field. It should show a selection frame and the right-hand panel should name it.
  3. Click through other page types. Each new type of component can bring a new error, and the diff tells you which cause it is.

Takeaways

  • A hydration error in the editor means the component is uneditable, not only that a warning appeared.
  • Read the diff: attributes and elements you chose point to your code, and contenteditable, ck-content or a text node turning into a span point to the editor.
  • Never use isEditorActive() in render. Use page.mode.isEditing, which is the same on the server and in the browser.
  • suppressHydrationWarning hides attribute and text differences on one element. It cannot hide a structural one.
  • Check your SDK version before working around editor markup. On 1.0, give a <Text> a tag; from 1.4 the SDK does it for you.
  • disableSuspense while editing has to reach every nested placeholder. A drop to one error after adding it is not proof the others are gone.
  • After a bulk change, run the type checker. A hook in a handler is a bug the compiler can find.