Create doc from snapshot

I am trying to create a doc from snapshot:

let snapshot: Y.Snapshot = Y.snapshot(ydoc);
    let snap: Uint8Array = Y.encodeSnapshot(snapshot);
    let content = String.fromCharCode(...new Uint8Array(snap));
    let snapBase64 = btoa(content);
    let lastsnapshot = localStorage.getItem("lastsnapshot");
    if (snapBase64 === lastsnapshot) {
      // never run into this
      return;
    }
    if (lastsnapshot) {
      let cached: Uint8Array = base64ToUint8Array(lastsnapshot);
      const decoded: Y.Snapshot = Y.decodeSnapshot(cached);
      const doc = new Y.Doc({ gc: false });
      const docRestored = Y.createDocFromSnapshot(doc, decoded);
      const stateVector = Y.encodeStateVector(docRestored);
      const diff:Uint8Array = Y.encodeStateAsUpdate(ydoc, stateVector);
      if (diff.length > 0) {
        let content = String.fromCharCode(...new Uint8Array(diff));
        console.log("diff content", content);
        debugger;
      }
      let equal = Y.equalSnapshots(decoded, snapshot);
      if (equal) {
        // never run into this
        return;
      }
    }

but shows error:


YjsEvent.ts:49 
TypeError: Cannot read properties of undefined (reading 'id')
    at findIndexSS (StructStore.js:115:22)
    at Snapshot.js:196:31
    at transact (Transaction.js:425:14)
    at _Doc.transact (Doc.js:184:12)
    at Module.createDocFromSnapshot (Snapshot.js:179:13)
    at handleYDocUpdate (YjsEvent.ts:19:29)
    at CollarEditorService.ts:151:5
    at observable.js:82:90
    at Array.forEach (<anonymous>)
    at _Doc.emit (observable.js:82:77)
handleYDocUpdate	@	YjsEvent.ts:49

is it possible to create from snapshot and get diff content? am I doing it the right way? The legacy way I am compare the text content diff, and store the full context into database, now I am trying to compare the diff delta and store the diff into database. seems create doc failed from snapshot.