Skip to content

Commit db0ed16

Browse files
authored
perf: Avoid unnecessary editor assets requests (#126)
* refactor: Remove unused WP dependencies return The usage was removed in earlier commits. * perf: Avoid unnecessary editor assets requests We now utilize the `loadEditorAssets` utility twice--once to load i18n modules, again to load the remaining `@wordpress` modules. The second invocation occurs immediately after the first, and there is no benefit from making another network request.
1 parent fdfe788 commit db0ed16

File tree

1 file changed

+54
-31
lines changed

1 file changed

+54
-31
lines changed

src/utils/remote-editor.js

+54-31
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ import apiFetch from '@wordpress/api-fetch';
99
import { getGBKit } from './bridge';
1010
import { error } from './logger';
1111

12+
/**
13+
* Cache for editor assets to avoid unnecessary network requests
14+
* @type {Object|null}
15+
*/
16+
let editorAssetsCache = null;
17+
1218
/**
1319
* @typedef {Object} EditorAssetConfig
1420
*
1521
* @property {string[]} allowedBlockTypes Array of allowed block types provided by the API.
16-
* @property {Object} wpDependencies WordPress dependencies, empty when allowedPackages is provided.
1722
*/
1823

1924
/**
@@ -23,50 +28,34 @@ import { error } from './logger';
2328
* @param {Array} [options.allowedPackages] Array of allowed package names to load.
2429
* @param {Array} [options.disallowedPackages] Array of disallowed package names to load.
2530
*
26-
* @return {EditorAssetConfig} Allowed block types and WordPress dependencies.
31+
* @return {EditorAssetConfig} Editor configuration provided by the API.
2732
*/
2833
export async function loadEditorAssets( {
2934
allowedPackages = [],
3035
disallowedPackages = [],
3136
} = {} ) {
3237
try {
38+
// Return cached response if available
39+
if ( editorAssetsCache ) {
40+
return processEditorAssets( editorAssetsCache, {
41+
allowedPackages,
42+
disallowedPackages,
43+
} );
44+
}
45+
3346
const { siteApiRoot, siteApiNamespace } = getGBKit();
3447
// TODO: Load editor assets within the host app
35-
const {
36-
styles,
37-
scripts,
38-
allowed_block_types: allowedBlockTypes,
39-
} = await apiFetch( {
48+
const response = await apiFetch( {
4049
url: `${ siteApiRoot }wpcom/v2/${ siteApiNamespace[ 0 ] }/editor-assets`,
4150
} );
4251

43-
if ( allowedPackages.length > 0 ) {
44-
// Only load allowed packages.
45-
await loadAssets( [ ...styles, ...scripts ].join( '' ), {
46-
allowedPackages,
47-
} );
48-
49-
return {
50-
allowedBlockTypes,
51-
wpDependencies: {},
52-
};
53-
}
52+
// Cache the response
53+
editorAssetsCache = response;
5454

55-
await loadAssets( [ ...styles, ...scripts ].join( '' ), {
55+
return processEditorAssets( response, {
56+
allowedPackages,
5657
disallowedPackages,
5758
} );
58-
59-
return {
60-
allowedBlockTypes,
61-
wpDependencies: {
62-
StrictMode: window.wp?.element?.StrictMode,
63-
createRoot: window.wp?.element?.createRoot,
64-
dispatch: window.wp?.data?.dispatch,
65-
editorStore: window.wp?.editor?.store,
66-
preferencesStore: window.wp?.preferences?.store,
67-
registerCoreBlocks: window.wp?.blockLibrary?.registerCoreBlocks,
68-
},
69-
};
7059
} catch ( err ) {
7160
error( 'Error loading editor assets', err );
7261
// Fallback to the local editor and display a notice. Because the remote
@@ -76,6 +65,40 @@ export async function loadEditorAssets( {
7665
}
7766
}
7867

68+
/**
69+
* Process editor assets and return the configuration
70+
*
71+
* @param {Object} assets The assets to process
72+
* @param {string[]} assets.styles Array of style assets
73+
* @param {string[]} assets.scripts Array of script assets
74+
* @param {string[]} assets.allowedBlockTypes Array of allowed block types
75+
* @param {Object} options Processing options
76+
* @param {string[]} options.allowedPackages Array of allowed package names
77+
* @param {string[]} options.disallowedPackages Array of disallowed package names
78+
*
79+
* @return {EditorAssetConfig} Processed editor configuration
80+
*/
81+
async function processEditorAssets(
82+
assets,
83+
{ allowedPackages = [], disallowedPackages = [] } = {}
84+
) {
85+
const { styles, scripts, allowed_block_types: allowedBlockTypes } = assets;
86+
87+
if ( allowedPackages.length > 0 ) {
88+
await loadAssets( [ ...styles, ...scripts ].join( '' ), {
89+
allowedPackages,
90+
} );
91+
92+
return { allowedBlockTypes };
93+
}
94+
95+
await loadAssets( [ ...styles, ...scripts ].join( '' ), {
96+
disallowedPackages,
97+
} );
98+
99+
return { allowedBlockTypes };
100+
}
101+
79102
/**
80103
* Load the asset files for a block
81104
*

0 commit comments

Comments
 (0)