Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(page): keep page rooms seeding and projecting on serverless builds (#21919)

Innei798fd6c0566e
diff --git a/apps/server/src/router-hono/agent/handlers/documentCollab.ts b/apps/server/src/router-hono/agent/handlers/documentCollab.ts
index 67687f72c5e..1896064c645 100644
--- a/apps/server/src/router-hono/agent/handlers/documentCollab.ts
+++ b/apps/server/src/router-hono/agent/handlers/documentCollab.ts
@@ -43,6 +43,7 @@ export async function documentProjection(c: Context): Promise<Response> {
if (error instanceof PageProjectionRejectedError) {
return c.json({ error: error.message }, 409);
}
+ console.error('[document-projection] failed for %s:', body.documentId, error);
return c.json({ error: (error as Error).message }, 500);
}
}
diff --git a/apps/server/src/services/pageCollab/__tests__/bootstrapOrder.test.ts b/apps/server/src/services/pageCollab/__tests__/bootstrapOrder.test.ts
index 6133e607bd6..d5c21607127 100644
--- a/apps/server/src/services/pageCollab/__tests__/bootstrapOrder.test.ts
+++ b/apps/server/src/services/pageCollab/__tests__/bootstrapOrder.test.ts
@@ -3,11 +3,10 @@ import { createHeadlessEditor } from '@lobehub/editor/headless';
import { expect, it } from 'vitest';
import * as Y from 'yjs';
-import { loadEditorYjs } from '../editorYjs';
+import { YjsPlugin } from '../editorYjs';
import { projectPageState } from '../fork';
it('bootstraps once when content is loaded after the provider synced', async () => {
- const { YjsPlugin } = await loadEditorYjs();
const listeners = new Map<string, Set<(...a: any[]) => void>>();
const provider: any = {
awareness: {
diff --git a/apps/server/src/services/pageCollab/editorYjs.ts b/apps/server/src/services/pageCollab/editorYjs.ts
index 2904b155349..89870f4bfef 100644
--- a/apps/server/src/services/pageCollab/editorYjs.ts
+++ b/apps/server/src/services/pageCollab/editorYjs.ts
@@ -1,32 +1,13 @@
-import { createRequire } from 'node:module';
-import path from 'node:path';
-import { pathToFileURL } from 'node:url';
+import type { HeadlessEditorOptions } from '@lobehub/editor/headless';
+import { YjsPlugin as EditorYjsPlugin } from '@lobehub/editor/yjs';
-interface EditorYjsModules {
- IYjsService: any;
- YjsPlugin: any;
-}
+export { IYjsService } from '@lobehub/editor/yjs';
-let modules: Promise<EditorYjsModules> | undefined;
+type HeadlessPluginConstructor = Extract<
+ NonNullable<HeadlessEditorOptions['additionalPlugins']>[number],
+ abstract new (...args: any[]) => unknown
+>;
-const EDITOR_PACKAGE = ['@lobehub', 'editor', 'package.json'].join('/');
-
-// @lobehub/editor exposes the Yjs plugin only from its browser entry, which
-// imports JSON without import attributes and fails under Node ESM; the plugin
-// modules themselves are Node-safe, so load them by file URL. The specifier is
-// built at runtime so the server bundler does not rewrite it into an external id.
-export const loadEditorYjs = () =>
- (modules ??= (async () => {
- const require = createRequire(path.join(process.cwd(), 'package.json'));
- const root = path.dirname(require.resolve(EDITOR_PACKAGE));
- const load = (file: string) =>
- import(
- /* webpackIgnore: true */ /* turbopackIgnore: true */ pathToFileURL(path.join(root, file))
- .href
- );
- const [plugin, service] = await Promise.all([
- load('es/plugins/yjs/plugin/index.js'),
- load('es/plugins/yjs/service/index.js'),
- ]);
- return { IYjsService: service.IYjsService, YjsPlugin: plugin.YjsPlugin };
- })());
+// `./headless` ships its own bundled copy of the kernel types, so the plugin
+// class from `./yjs` is the same constructor at runtime but a distinct type.
+export const YjsPlugin = EditorYjsPlugin as unknown as HeadlessPluginConstructor;
diff --git a/apps/server/src/services/pageCollab/fork.ts b/apps/server/src/services/pageCollab/fork.ts
index c6acc61d750..2cd9179d64e 100644
--- a/apps/server/src/services/pageCollab/fork.ts
+++ b/apps/server/src/services/pageCollab/fork.ts
@@ -4,7 +4,7 @@ import * as Y from 'yjs';
import { createDocumentHeadlessEditor } from '@/server/services/agentDocuments/headlessEditor';
-import { loadEditorYjs } from './editorYjs';
+import { IYjsService, YjsPlugin } from './editorYjs';
import type { PageDraft } from './session';
type SerializedEditor = SerializedEditorState<SerializedLexicalNode>;
@@ -62,7 +62,6 @@ export const openPageFork = async (
state: Uint8Array | undefined,
bootstrapFrom?: SerializedEditor | null,
): Promise<PageFork> => {
- const { IYjsService, YjsPlugin } = await loadEditorYjs();
const doc = new Y.Doc();
if (state && state.length > 0) Y.applyUpdate(doc, state);
const baseVector = Y.encodeStateVector(doc);
diff --git a/package.json b/package.json
index a3f4692bf3d..973283c4cf9 100644
--- a/package.json
+++ b/package.json
@@ -326,7 +326,7 @@
"@lobehub/analytics": "^1.6.4",
"@lobehub/charts": "^6.0.0",
"@lobehub/desktop-ipc-typings": "workspace:*",
- "@lobehub/editor": "7.0.2",
+ "@lobehub/editor": "7.0.4",
"@lobehub/icons": "^6.13.0",
"@lobehub/market-sdk": "0.43.0",
"@lobehub/ui": "^6.9.0",
diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml
index d11b0316ed5..7b52b9c4470 100644
--- a/pnpm-workspace.yaml
+++ b/pnpm-workspace.yaml
@@ -65,8 +65,9 @@ allowBuilds:
overrides:
'@lobehub/ui': ^6.9.0
'@lobehub/icons': ^6.0.0
- # 7.0.3 moves to lexical 0.53 while the root still pins lexical 0.42; lift both together.
- '@lobehub/editor': 7.0.2
+ # Exact: an editor release can move its lexical version, and the root pins
+ # lexical; lift both together.
+ '@lobehub/editor': 7.0.4
'@lobehub/charts': ^6.0.0
'@lobehub/fluent-emoji': ^5.0.0
# Stay on the latest 1.9.x: @opentelemetry/*@2.x peers reject 1.10.0, and mixing both splits drizzle-orm into duplicate peer variants.
diff --git a/src/features/PageEditor/collab/provider.test.ts b/src/features/PageEditor/collab/provider.test.ts
index 2edc87c4a42..80aa2882b89 100644
--- a/src/features/PageEditor/collab/provider.test.ts
+++ b/src/features/PageEditor/collab/provider.test.ts
@@ -223,6 +223,25 @@ describe('PageCollabProvider', () => {
expect(updates()[2].bootstrap).toBeUndefined();
});
+ it('flags what the editor writes into an empty room on sync while holding the claim', () => {
+ const { deliver, mux, ready, sent } = createMux();
+ const doc = new Y.Doc();
+ const provider = new PageCollabProvider('doc_1', doc, mux as any);
+ provider.on('sync', () => doc.getText('t').insert(0, 'root paragraph'));
+
+ provider.connect();
+ ready();
+ deliver(
+ synced({
+ bootstrap: true,
+ bootstrapped: false,
+ update: b64(Y.encodeStateAsUpdate(new Y.Doc())),
+ }),
+ );
+
+ expect(sent.filter((m) => m.type === 'doc_update').map((m) => m.bootstrap)).toEqual([true]);
+ });
+
it('resets once when it lost the bootstrap race', () => {
const { deliver, mux, ready } = createMux();
const onReset = vi.fn();
diff --git a/src/features/PageEditor/collab/provider.ts b/src/features/PageEditor/collab/provider.ts
index c0d5ea21d3e..2788b71bfc2 100644
--- a/src/features/PageEditor/collab/provider.ts
+++ b/src/features/PageEditor/collab/provider.ts
@@ -243,7 +243,6 @@ export class PageCollabProvider extends Emitter {
queueMicrotask(() => {
if (!this.bootstrapPending) return;
this.bootstrapPending = false;
- this.bootstrapping = true;
this.events.onBootstrap?.();
});
}
@@ -274,6 +273,9 @@ export class PageCollabProvider extends Emitter {
}
this.resendPending();
if (message.bootstrap || message.bootstrapped) {
+ // The binding seeds an empty root as soon as it hears `sync`, before
+ // the bootstrap below runs; that write is part of our bootstrap too.
+ this.bootstrapping = message.bootstrap;
this.markSynced();
if (message.bootstrap) this.scheduleBootstrap();
} else {