@@ -9,11 +9,16 @@ import apiFetch from '@wordpress/api-fetch';
9
9
import { getGBKit } from './bridge' ;
10
10
import { error } from './logger' ;
11
11
12
+ /**
13
+ * Cache for editor assets to avoid unnecessary network requests
14
+ * @type {Object|null }
15
+ */
16
+ let editorAssetsCache = null ;
17
+
12
18
/**
13
19
* @typedef {Object } EditorAssetConfig
14
20
*
15
21
* @property {string[] } allowedBlockTypes Array of allowed block types provided by the API.
16
- * @property {Object } wpDependencies WordPress dependencies, empty when allowedPackages is provided.
17
22
*/
18
23
19
24
/**
@@ -23,50 +28,34 @@ import { error } from './logger';
23
28
* @param {Array } [options.allowedPackages] Array of allowed package names to load.
24
29
* @param {Array } [options.disallowedPackages] Array of disallowed package names to load.
25
30
*
26
- * @return {EditorAssetConfig } Allowed block types and WordPress dependencies .
31
+ * @return {EditorAssetConfig } Editor configuration provided by the API .
27
32
*/
28
33
export async function loadEditorAssets ( {
29
34
allowedPackages = [ ] ,
30
35
disallowedPackages = [ ] ,
31
36
} = { } ) {
32
37
try {
38
+ // Return cached response if available
39
+ if ( editorAssetsCache ) {
40
+ return processEditorAssets ( editorAssetsCache , {
41
+ allowedPackages,
42
+ disallowedPackages,
43
+ } ) ;
44
+ }
45
+
33
46
const { siteApiRoot, siteApiNamespace } = getGBKit ( ) ;
34
47
// 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 ( {
40
49
url : `${ siteApiRoot } wpcom/v2/${ siteApiNamespace [ 0 ] } /editor-assets` ,
41
50
} ) ;
42
51
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 ;
54
54
55
- await loadAssets ( [ ...styles , ...scripts ] . join ( '' ) , {
55
+ return processEditorAssets ( response , {
56
+ allowedPackages,
56
57
disallowedPackages,
57
58
} ) ;
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
- } ;
70
59
} catch ( err ) {
71
60
error ( 'Error loading editor assets' , err ) ;
72
61
// Fallback to the local editor and display a notice. Because the remote
@@ -76,6 +65,40 @@ export async function loadEditorAssets( {
76
65
}
77
66
}
78
67
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
+
79
102
/**
80
103
* Load the asset files for a block
81
104
*
0 commit comments