Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core-data/src/hooks/use-entity-block-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { parse, __unstableSerializeAndClean } from '@wordpress/blocks';
import { STORE_NAME } from '../name';
import useEntityId from './use-entity-id';
import { updateFootnotesFromMeta } from '../footnotes';
import { parsedBlocksCache, getCacheKey } from '../parsed-blocks-cache';

const EMPTY_ARRAY = [];
const parsedBlocksCache = new Map();

/**
* Hook that returns block content getters and setters for
Expand Down Expand Up @@ -69,7 +69,7 @@ export default function useEntityBlockEditor( kind, name, { id: _id } = {} ) {

// Cache parsed blocks by entity identity. Store the content
// alongside the blocks so we can validate it hasn't changed.
const cacheKey = `${ kind }:${ name }:${ id }`;
const cacheKey = getCacheKey( kind, name, id );
const cached = parsedBlocksCache.get( cacheKey );
let _blocks;

Expand Down
12 changes: 12 additions & 0 deletions packages/core-data/src/parsed-blocks-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Shared cache of blocks parsed from an entity's `content` string, keyed by
* `kind:name:id`. Populated both eagerly by the `getEntityRecord` resolver
* (when the sync manager parses content for transient edits) and lazily by
* `useEntityBlockEditor`. The stored `content` string acts as a validator so
* stale entries are discarded when the underlying record changes.
*/
export const parsedBlocksCache = new Map();

export function getCacheKey( kind, name, id ) {
return `${ kind }:${ name }:${ id }`;
}
13 changes: 13 additions & 0 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from './utils';
import { fetchBlockPatterns } from './fetch';
import { restoreSelection, getSelectionHistory } from './utils/crdt-selection';
import { parsedBlocksCache, getCacheKey } from './parsed-blocks-cache';

/**
* Requests authors from the REST API.
Expand Down Expand Up @@ -180,6 +181,18 @@ export const getEntityRecord =
transientConfig.read( recordWithTransients );
} );

// Share the parsed blocks with `useEntityBlockEditor` so the
// editor doesn't re-parse the same `content` string.
if (
recordWithTransients.blocks &&
typeof recordWithTransients.content?.raw === 'string'
) {
parsedBlocksCache.set( getCacheKey( kind, name, key ), {
content: recordWithTransients.content.raw,
blocks: recordWithTransients.blocks,
} );
}

// Load the entity record for syncing. Do not await promise.
void getSyncManager()?.load(
entityConfig.syncConfig,
Expand Down
Loading