Files
Tom MoorandClaude Opus 5 eaad1a71dc React SSR editor nodes (#13113)
* Render React node views in server-side HTML export

Passes nodeViews to the headless EditorView used for export so that nodes
with a React component render with it, matching the editor, and falling
back to toDOM when a component cannot render server-side.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Let nodes opt out of component rendering in static HTML

Replaces the hardcoded set of node names in the server nodeViews factory with
an allowComponentInStaticHTML getter on Extension, so a node declares the
behavior alongside its own definition rather than in a server file that does
not know about it.

Mention opts out: its component requires stores and a router, so it always
threw and fell back to toDOM, at the cost of a React render, an exception and
a log line per node. A document with 100 mentions now exports in ~12ms rather
than ~25ms and emits no warnings.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* Yield one less macrotask turn when flushing React effects

Only two turns are needed: the first flushes passive effects, the second fires
Frame's own setTimeout(0) whose setState commits the iframe. The third turn
absorbed nothing that the components produce.

Each turn is roughly a millisecond of idle time held inside the render lock, on
every export — including plain documents, share page SSR and notification
emails — so dropping it takes a small export from ~6ms to ~4ms.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-16 16:36:13 -04:00

83 lines
2.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { DOMSerializer, type Node as ProsemirrorNode } from "prosemirror-model";
import type {
Decoration,
EditorView,
NodeView,
NodeViewConstructor,
} from "prosemirror-view";
import type { ServerStyleSheet } from "styled-components";
import type ReactNode from "@shared/editor/nodes/ReactNode";
import { errToString } from "@shared/utils/error";
import { extensionManager } from "@server/editor";
import Logger from "@server/logging/Logger";
import { ComponentView } from "./ComponentView";
/**
* Renders a node with its plain `toDOM` spec – today's server export output –
* as a static NodeView. Used for nodes that prefer toDOM, and as the fallback
* when a component unexpectedly fails to render server-side.
*/
function renderToDOMFallback(node: ProsemirrorNode): NodeView {
const toDOM = node.type.spec.toDOM;
const tag = node.type.spec.inline ? "span" : "div";
const rendered = toDOM
? DOMSerializer.renderSpec(document, toDOM(node))
: { dom: document.createElement(tag), contentDOM: undefined };
return {
// renderSpec is typed to return a generic DOM node; our node specs always
// produce an element.
dom: rendered.dom as HTMLElement,
contentDOM: rendered.contentDOM,
update: () => false,
ignoreMutation: () => true,
};
}
/**
* Builds the set of ProseMirror NodeViews used during server HTML export,
* mirroring the browser editor: every extension with a React `component`
* renders through {@link ComponentView}. A component that throws on
* construction degrades gracefully to its `toDOM` output.
*
* @param sheet The per-export styled-components sheet the components render into.
* @returns A map of node name to NodeView constructor.
*/
export function createNodeViews(
sheet: ServerStyleSheet
): Record<string, NodeViewConstructor> {
return Object.fromEntries(
extensionManager.extensions
.filter((extension: ReactNode) => extension.component)
.filter((extension: ReactNode) => extension.allowComponentInStaticHTML)
.map((extension: ReactNode) => [
extension.name,
(
node: ProsemirrorNode,
view: EditorView,
getPos: () => number | undefined,
decorations: readonly Decoration[]
): NodeView => {
try {
return new ComponentView(extension.component, {
node,
view,
getPos,
decorations,
sheet,
});
} catch (err) {
Logger.warn(
"Component view failed to render, falling back to toDOM",
{
node: node.type.name,
error: errToString(err),
}
);
return renderToDOMFallback(node);
}
},
])
) as Record<string, NodeViewConstructor>;
}