diff --git a/packages/sync/src/manager.ts b/packages/sync/src/manager.ts index 26da50015bef81..cd53b98485663d 100644 --- a/packages/sync/src/manager.ts +++ b/packages/sync/src/manager.ts @@ -610,11 +610,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 ccfbb65f4a9e4c..a73569f08a9f41 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 ); + } ); } ); } );