@@ -9,6 +9,12 @@ 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
*
@@ -29,30 +35,27 @@ export async function loadEditorAssets( {
29
35
disallowedPackages = [ ] ,
30
36
} = { } ) {
31
37
try {
38
+ // Return cached response if available
39
+ if ( editorAssetsCache ) {
40
+ return processEditorAssets ( editorAssetsCache , {
41
+ allowedPackages,
42
+ disallowedPackages,
43
+ } ) ;
44
+ }
45
+
32
46
const { siteApiRoot, siteApiNamespace } = getGBKit ( ) ;
33
47
// 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 ( {
39
49
url : `${ siteApiRoot } wpcom/v2/${ siteApiNamespace [ 0 ] } /editor-assets` ,
40
50
} ) ;
41
51
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 ;
50
54
51
- await loadAssets ( [ ...styles , ...scripts ] . join ( '' ) , {
55
+ return processEditorAssets ( response , {
56
+ allowedPackages,
52
57
disallowedPackages,
53
58
} ) ;
54
-
55
- return { allowedBlockTypes } ;
56
59
} catch ( err ) {
57
60
error ( 'Error loading editor assets' , err ) ;
58
61
// Fallback to the local editor and display a notice. Because the remote
@@ -62,6 +65,40 @@ export async function loadEditorAssets( {
62
65
}
63
66
}
64
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
+
65
102
/**
66
103
* Load the asset files for a block
67
104
*
0 commit comments