From d5363f41c0bd50cd3d8203a2de6bb1b50f5fb09e Mon Sep 17 00:00:00 2001 From: Gitesh Date: Wed, 8 Jul 2026 21:20:28 +0530 Subject: [PATCH] RTC: Preserve unsaved blocks when reconciling a persisted CRDT doc When 'blocks' is an invalidated key during CRDT doc reconciliation, record['blocks'] is always undefined since REST post records only expose 'content'. Without an accompanying content value, the CRDT apply logic wiped the blocks key instead of merging via the existing mergeContentWithoutBlocks path, causing duplicated content on repeated reconciliation (e.g. viewing a server-side autosave). Carry record.content along whenever blocks is invalidated so the existing merge path in crdt.ts gets what it needs. Fixes #80004 --- packages/sync/src/manager.ts | 17 ++++++++---- packages/sync/src/test/manager.ts | 43 +++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/packages/sync/src/manager.ts b/packages/sync/src/manager.ts index 9d72f43dee83a3..cb8fd0a3e84fb2 100644 --- a/packages/sync/src/manager.ts +++ b/packages/sync/src/manager.ts @@ -606,11 +606,18 @@ export function createSyncManager( debug = false ): SyncManager { } ); // Use the invalidated keys to get the updated values from the entity. - const changes = invalidatedKeys.reduce( - ( acc, key ) => - Object.assign( acc, { - [ key ]: record[ key ], - } ), + const changes = invalidatedKeys.reduce< Partial< ObjectData > >( + ( acc, key ) => { + acc[ key ] = record[ key ]; + // The entity record never exposes a `blocks` property (only + // serialized `content`), so also carry `content` along to let + // `applyPostChangesToCRDTDoc` re-derive blocks from content + // instead of wiping them. + if ( key === 'blocks' && record.content ) { + acc.content = record.content; + } + return acc; + }, {} ); diff --git a/packages/sync/src/test/manager.ts b/packages/sync/src/test/manager.ts index f0d2e69fb0adb2..5f6ed06a64da60 100644 --- a/packages/sync/src/test/manager.ts +++ b/packages/sync/src/test/manager.ts @@ -418,6 +418,49 @@ describe( 'SyncManager', () => { 1 ); } ); + + it( 'carries record.content along when blocks is an invalidated field', async () => { + mockRecord = { + ...mockRecord, + content: 'Test content', + }; + + mockSyncConfig = { + ...mockSyncConfig, + getPersistedCRDTDoc: jest.fn( () => + createPersistedCRDTDoc( { + ...mockRecord, + blocks: [ { name: 'core/paragraph' } ], + } ) + ), + }; + + const manager = createSyncManager(); + + await manager.load( + mockSyncConfig, + 'post', + '123', + mockRecord, + mockHandlers + ); + + // The entity record never exposes a `blocks` property, so the + // change for the invalidated `blocks` key should also carry + // `content` along, letting the CRDT doc re-derive blocks from + // content instead of wiping them. + const expectedChanges = { + blocks: undefined, + content: mockRecord.content, + }; + + expect( + mockSyncConfig.applyChangesToCRDTDoc + ).toHaveBeenCalledTimes( 1 ); + expect( + mockSyncConfig.applyChangesToCRDTDoc + ).toHaveBeenCalledWith( expect.any( Y.Doc ), expectedChanges ); + } ); } ); } );