BitPhinix / slate-yjs

Yjs binding for Slate
https://docs.slate-yjs.dev
MIT License
514 stars 73 forks source link

when using slate-yjs and y-indexeddb,Data duplication on server restart #420

Open LT1211 opened 9 months ago

LT1211 commented 9 months ago

Hi @BitPhinix

I have a similar issue when using slate-yjs with y-indexeddb. Each time the slate editor is mounted, the contents are duplicated. The indexeddb updates table also seems to double in size each time the page is loaded. I'm not doing anything special here. Just passing along the y doc which holds my sharedType...

This is frontend code

 useEffect(() => {
    new IndexeddbPersistence(docName, provider.document)
  }, [])

This is backend code

  async onLoadDocument(data) {
    if (data.document.isEmpty(data.documentName)) {
      const insertDelta = slateNodesToInsertDelta(initialValue)
      const sharedRoot = data.document.get(data.documentName, Y.XmlText) as unknown as Y.XmlText
      sharedRoot.applyDelta(insertDelta)
    }
    return data.document
  }
beeant0512 commented 4 months ago

i am using hocuspocus as backend server, and here is my solution to fix the issue

    async onLoadDocument(data) {
        if (data.document.isEmpty('content')) {
            const result = await selectYjsDocument(data.documentName);
            if (result) {
                Y.applyUpdate(
                    data.document,
                    fromBase64(result),
                    'snapshot-patch'
                );
            }
        }
        return data.document;
    }
dpnova commented 1 day ago

If hocuspocus is using sqlite - it would empty its local db on server restart, then pull the content from your remote server.

In this case it would think the document is empty and insert the remote content again, then serve that to the client, which also has the content in indexeddb. This would result in dupe content.

if you're using sqlite, try switching to a db that wont get wiped on server restart