Server rendering
Load the editor on the client, and render saved documents on the server.
The editor is a client component. It needs the DOM, so a Server Component should not import it. A saved document is JSON. renderDocument turns that JSON into HTML with no DOM and no React, which is what a Server Component, a route handler, or a Node script can return.
The files in examples/ssr are typechecked with the editor (pnpm --dir editor typecheck). savedDocumentHtml is also executed in Node by addonRender.node.test.ts.
Client-only editor in Next.js
Keep the editor out of the server bundle with next/dynamic and ssr: false.
"use client";
import dynamic from "next/dynamic";
const BlockEditor = dynamic(() => import("./block-editor"), { ssr: false });
export function EditorSlot() {
return <BlockEditor />;
}block-editor.tsx is the file that calls useEditor and renders <Editor>. The server sends a placeholder. The editor chunk loads in the browser. examples/ssr/client-editor.tsx is the same boundary without importing next, so CI can typecheck it inside the editor project.
App Router and Vite allow the editor's CSS imports. The Pages Router does not: move editor.css, ai.css, collaboration.css, Excalidraw's CSS, and KaTeX's CSS into pages/_app. See Supported stack.
Render a saved document
import { renderDocument } from "@/lib/export/serialize";
import { mediaSerializers } from "@/components/editor/mediaSerialize";
import { mathSerializers } from "@/components/editor/addons/mathSerialize";
import { columnsSerializers } from "@/components/editor/addons/columnsSerialize";
import { tocSerializers } from "@/components/editor/addons/tocSerialize";
import { excalidrawSerializers } from "@/components/editor/excalidraw/exportDrawings";
export function pageHtml(json: JSONContent) {
return renderDocument(json, {
addons: [
{ serializers: mediaSerializers },
{ serializers: excalidrawSerializers },
{ serializers: mathSerializers },
{ serializers: columnsSerializers },
{ serializers: tocSerializers },
],
});
}Pass the add-ons the document was written with. A block with no serializer and no children becomes empty HTML when the schema is not available, or the schema's renderHTML when you pass a schema into the lower-level serializeHtmlBody. Tables, headings, lists, callouts, and toggles use the core HTML map. Mentions and inline math do too: they are inline, so they render inside the paragraph without an add-on entry. Ask AI and collaboration do not add document nodes.
examples/ssr/render-saved.ts builds one document that contains an image, video, audio, file, link, embed, drawing, math block, inline math, columns, a mention, a table of contents, and a table, then renders it.
Drawings without a PNG data URL render as the word "Drawing". To embed the picture, export from the live editor (ExportMenu), which rasterises the scene first. renderDocument itself does not load Excalidraw.
Hydrate read-only content
A public page can paint the HTML immediately and mount a read-only editor after load:
export function Article({ json }: { json: JSONContent }) {
const html = pageHtml(json);
return <article dangerouslySetInnerHTML={{ __html: html }} />;
}When the reader should get the real editor (copy, comments later, or an "Edit" button), load the client editor with the same JSON and editable: false:
const editor = useEditor({ extensions, content: json, editable: false });Do not hydrate the server HTML into the ProseMirror tree. ProseMirror builds its own DOM from the JSON. Show the server HTML, then replace that node when the client editor is ready. ReadOnlyDocument in examples/ssr/client-editor.tsx does that swap.
Streaming
In the App Router, a page can render the article HTML in the initial RSC payload and stream the editor as a separate client boundary. dynamic(..., { ssr: false }) is that boundary: the shell and the renderDocument HTML flush first, and the editor chunk arrives after.
import { Suspense } from "react";
export default async function Page({ params }: { params: Promise<{ id: string }> }) {
const json = await loadDocument((await params).id);
return (
<>
<article dangerouslySetInnerHTML={{ __html: pageHtml(json) }} />
<Suspense fallback={null}>
<EditorSlot />
</Suspense>
</>
);
}Leave the editor out of the Suspense fallback that readers see as the article. The article is the renderDocument output.
Caching
The HTML depends only on the JSON and the serializer set. Cache it by document id and revision. Do not cache the editable editor.
import { unstable_cache } from "next/cache";
const cachedHtml = unstable_cache(
async (id: string, revision: number) => pageHtml(await loadRevision(id, revision)),
["document-html"],
{ tags: ["document-html"] },
);Invalidate the tag when a save bumps the revision. A CDN can also cache GET /api/docs/:id.html with Cache-Control: public, s-maxage=60 and a revision in the URL (?v=12) so a new save is a new URL. Collaborative rooms should not use that cache for the live editor; the WebSocket document is the source of truth, and renderDocument is for the saved snapshot.