Collaboration (Beta)

Use comments in Next.js

Render the comments panel in the Next.js App Router: the client boundary, the stylesheet import, and the styled-components setup.

The comments panel is the same in Next.js as in any React app. What changes is where the client boundary goes, where the stylesheet is imported, and what styled-components needs from the App Router. This guide covers those three things, so that a panel you have already built renders correctly in the App Router.

Prerequisites:

  • A working comments panel. See Get started.
  • A Next.js app on the App Router, running Next.js 16 or later. With TypeScript 7, you need Next.js 16.3.0 or later; on earlier 16.x releases, use TypeScript 5.

Mark the client boundary once

The components use context, refs, and an editor, and the SDK hooks subscribe. None of that runs on the server, so the panel is a client component.

Mark the boundary once, as high as the panel. This is the Comments.tsx from Get started, reading its document from config:

'use client' doesn't stop Next.js from prerendering the component on the server, and SanityApp reads window while it renders, so next build fails with window is not defined. Load App in the browser only, with next/dynamic and ssr: false. Next.js only allows ssr: false in a client component, so it goes in a small wrapper:

The page stays a server component and renders the wrapper:

App is the one from Get started, with the project and dataset read from config:

Server components can render around the wrapper, including the page, the document body, and the layout. They can't be imported into a client component.

Next.js only exposes environment variables prefixed with NEXT_PUBLIC_ to the browser, and only inlines process.env references it can see written out in full:

Set them in .env:

The Next.js development server runs on http://localhost:3000. Add that origin, with credentials allowed, to the project's CORS origins at sanity.io/manage.

Import the stylesheet in the root layout

Sanity UI ships its static styles as a plain stylesheet, and the root layout is where the App Router expects global CSS:

Enable the styled-components compiler

styled-components needs Next.js's compiler support turned on. Without it you get hydration mismatches on anything the components style:

Add a styled-components registry

styled-components also needs a style registry in the App Router, to collect styles during the server render and flush them into the document head. Without one, the comment styles flash unstyled on first paint.

This is Next.js's documented pattern, reproduced here so the panel setup is in one place. How to use CSS-in-JS libraries is the authority if it changes:

Wrap the body's children in it:

If your app already has a registry, the panel needs nothing further.

Render on the server

There is no window during a server render, which is one reason the package never reads the URL itself. The address is passed in as a value, so it is available on the first client render without an effect. See Link to a comment for the App Router wiring, including the {scroll: false} that keeps the router from fighting the scroll the package performs.

Next steps

Was this page helpful?