Files
outline/server/editor/index.ts
T
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

85 lines
2.5 KiB
TypeScript

import data from "@emoji-mart/data";
import type { EmojiMartData } from "@emoji-mart/data";
import { Schema } from "prosemirror-model";
import type { Editor } from "~/editor";
import defaultEmbeds from "@shared/editor/embeds";
import ExtensionManager from "@shared/editor/lib/ExtensionManager";
import { populateEmojiData } from "@shared/editor/lib/emoji";
import {
basicExtensions,
richExtensions,
withComments,
} from "@shared/editor/nodes";
import CodeBlock from "@shared/editor/nodes/CodeBlock";
import CodeFence from "@shared/editor/nodes/CodeFence";
import Mention from "@shared/editor/nodes/Mention";
populateEmojiData(data as EmojiMartData);
// Server-side parsing/serializing only requires schema and a few static props,
// but the Extension API expects a full Editor. This stub satisfies bindEditor
// without instantiating the React component.
const stubEditor = (s: Schema): Editor =>
({
schema: s,
props: {
theme: { isDark: false },
embeds: defaultEmbeds,
embedsDisabled: false,
},
}) as unknown as Editor;
const extensions = withComments(richExtensions);
export const extensionManager = new ExtensionManager(extensions);
export const schema = new Schema({
nodes: extensionManager.nodes,
marks: extensionManager.marks,
});
for (const extension of extensionManager.extensions) {
extension.bindEditor(stubEditor(schema));
}
export const parser = extensionManager.parser({
schema,
plugins: extensionManager.rulePlugins,
});
export const serializer = extensionManager.serializer();
export const plugins = extensionManager.plugins;
export const basicExtensionManager = new ExtensionManager(basicExtensions);
export const basicSchema = new Schema({
nodes: basicExtensionManager.nodes,
marks: basicExtensionManager.marks,
});
for (const extension of basicExtensionManager.extensions) {
extension.bindEditor(stubEditor(basicSchema));
}
export const basicParser = basicExtensionManager.parser({
schema: basicSchema,
plugins: basicExtensionManager.rulePlugins,
});
const commentExtensions = [...basicExtensions, CodeBlock, CodeFence, Mention];
export const commentExtensionManager = new ExtensionManager(commentExtensions);
export const commentSchema = new Schema({
nodes: commentExtensionManager.nodes,
marks: commentExtensionManager.marks,
});
for (const extension of commentExtensionManager.extensions) {
extension.bindEditor(stubEditor(commentSchema));
}
export const commentParser = commentExtensionManager.parser({
schema: commentSchema,
plugins: commentExtensionManager.rulePlugins,
});