Skip to content

Commit 30c0cec

Browse files
committed
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 abec26b commit 30c0cec

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

src/utils/remote-editor.js

+53-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ 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
*
@@ -29,30 +35,27 @@ export async function loadEditorAssets( {
2935
disallowedPackages = [],
3036
} = {} ) {
3137
try {
38+
// Return cached response if available
39+
if ( editorAssetsCache ) {
40+
return processEditorAssets( editorAssetsCache, {
41+
allowedPackages,
42+
disallowedPackages,
43+
} );
44+
}
45+
3246
const { siteApiRoot, siteApiNamespace } = getGBKit();
3347
// TODO: Load editor assets within the host app
34-
const {
35-
styles,
36-
scripts,
37-
allowed_block_types: allowedBlockTypes,
38-
} = await apiFetch( {
48+
const response = await apiFetch( {
3949
url: `${ siteApiRoot }wpcom/v2/${ siteApiNamespace[ 0 ] }/editor-assets`,
4050
} );
4151

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

51-
await loadAssets( [ ...styles, ...scripts ].join( '' ), {
55+
return processEditorAssets( response, {
56+
allowedPackages,
5257
disallowedPackages,
5358
} );
54-
55-
return { allowedBlockTypes };
5659
} catch ( err ) {
5760
error( 'Error loading editor assets', err );
5861
// Fallback to the local editor and display a notice. Because the remote
@@ -62,6 +65,40 @@ export async function loadEditorAssets( {
6265
}
6366
}
6467

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+
65102
/**
66103
* Load the asset files for a block
67104
*

0 commit comments

Comments
 (0)