Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LT1211 opened this issue Feb 21, 2024 · 1 comment
Open

Comments

@LT1211
Copy link

LT1211 commented Feb 21, 2024

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
Copy link

beeant0512 commented Jul 18, 2024

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

  • save document to mongo, it is important to translate buffer to base64 string
        const insertRst = await collection.insertOne(
            {
                rid: documentName,
                data: data.toString('base64'),
                create_at: Date.now(),
            },
            {}
        );
  • load document from mongo
const fromBase64 = (str: string): Uint8Array =>
    new Uint8Array(Array.from(atob(str)).map((char) => char.charCodeAt(0)));
    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;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants