Skip to content

[BUG] Content duplicated when a page without ydoc is reopened (e.g. created from template) #2618

Closed

Description

@adrianwalthert

Description

Pages created from a template sometimes show the whole content twice after reopening them. Every heading and block is duplicated. This happened to two different users in our workspace.

The root cause is not template specific. It affects every page that has content but no ydoc in the database.

Steps to reproduce

  1. Self-hosted Docmost v0.97.0 (Docker image docmost/docmost:latest, pgvector/pgvector:pg18-bookworm, redis:8)
  2. Create a new page from a template
  3. Leave the page without editing it
  4. Open the page again in the same browser
  5. The content is now duplicated

Logs

Example page 01a18698-ed11-7a77-9485-53c6122b5c13:

07:38:51.727 PersistenceExtension  converting json to ydoc: 01a18698-ed11-...
07:39:17.942 CollabLoggerExtension Unloaded page.01a18698-ed11-... from memory
(no "Page updated" in between)
07:42:00.802 PersistenceExtension  converting json to ydoc: 01a18698-ed11-...
07:42:10.860 PersistenceExtension  Page updated: 01a18698-ed11-...   <- duplicated content saved

Pages created empty (creating fresh ydoc) are not affected.

Cause

apps/server/src/collaboration/extensions/persistence.extension.ts

  1. onLoadDocument builds a ydoc from page.content when page.ydoc is null, but never persists it. The result of Y.encodeStateAsUpdate(ydoc) is discarded.
  2. onStoreDocument returns early when the JSON equals page.content. For an unchanged page created from a template the ydoc is therefore never written.
  3. On the next cold load the server builds a second, independent ydoc from the same JSON.
  4. The browser still holds the first Yjs state in IndexedDB (page-editor.tsx, IndexeddbPersistence). When both states sync, Yjs treats them as separate inserts and the content ends up twice. This merged state is then persisted.

The same logic exists in v0.96.0, so this is not a v0.97.0 regression.

Suggested fix

Persist the converted state directly in onLoadDocument:

     if (page.content) {
       this.logger.debug(`converting json to ydoc: ${pageId}`);
 
       const ydoc = TiptapTransformer.toYdoc(
         page.content,
         'default',
         tiptapExtensions,
       );
 
-      Y.encodeStateAsUpdate(ydoc);
+      try {
+        await this.pageRepo.updatePage(
+          { ydoc: Buffer.from(Y.encodeStateAsUpdate(ydoc)) },
+          pageId,
+        );
+      } catch (err) {
+        this.logger.error(`Failed to persist initial ydoc for ${pageId}`, err);
+      }
+
       return ydoc;
     }

This also covers the case where a page is opened and left without any edit, so onStoreDocument never has a reason to save.

Alternatively /templates/use (and any other path that creates pages with JSON content only) could store the ydoc together with the content.

Workaround

Make a lasting edit (e.g. fill in a field) and wait for the first save before leaving a page created from a template. Typing and deleting again does not help, since the content is then identical to the template and nothing is stored.

Activity

  1. Philipinho commented on Oct 30, 2026

    @Philipinho
    Member

    Thanks for reporting. A fix will be available in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions