@@ -25,6 +25,18 @@ function toSlug(value, fallback = "asset") {
2525 return text || fallback ;
2626}
2727
28+ function createDomainIndex ( binding ) {
29+ const index = { } ;
30+ Object . entries ( binding ?. domains || { } ) . forEach ( ( [ domain , entries ] ) => {
31+ const domainMap = new Map ( ) ;
32+ ( Array . isArray ( entries ) ? entries : [ ] ) . forEach ( ( entry ) => {
33+ domainMap . set ( safeString ( entry ?. assetId , "" ) , entry ) ;
34+ } ) ;
35+ index [ domain ] = domainMap ;
36+ } ) ;
37+ return index ;
38+ }
39+
2840export function getRuntimeBindingDomain ( assetId ) {
2941 const normalizedId = safeString ( assetId , "" ) . toLowerCase ( ) ;
3042 const matchedPrefix = Object . keys ( RUNTIME_BINDING_PREFIXES )
@@ -75,6 +87,14 @@ export function createRuntimeManifestAssetLookup(options = {}) {
7587 records
7688 } ) ;
7789 const binding = createRuntimeAssetBinding ( coordinatedManifest . manifest ) ;
90+ const bindingIndex = createDomainIndex ( binding ) ;
91+ const manifestSnapshot = cloneJson ( coordinatedManifest . manifest ) ;
92+ const staticSourceCache = new Map ( ) ;
93+ const resolutionCache = new Map ( ) ;
94+ const cacheStats = {
95+ hits : 0 ,
96+ misses : 0
97+ } ;
7898 const missingBindingBehavior = options . missingBindingBehavior === "null" ? "null" : "static" ;
7999 const errors = [ ] ;
80100
@@ -105,27 +125,41 @@ export function createRuntimeManifestAssetLookup(options = {}) {
105125 return null ;
106126 }
107127
128+ if ( resolutionCache . has ( assetId ) ) {
129+ cacheStats . hits += 1 ;
130+ return resolutionCache . get ( assetId ) ;
131+ }
132+ cacheStats . misses += 1 ;
133+
108134 if ( safeString ( asset ?. type , "" ) === "image" ) {
109135 if ( typeof options . resolveImageAsset === "function" ) {
110- return options . resolveImageAsset ( asset ) ;
136+ const resolvedImage = options . resolveImageAsset ( asset ) ;
137+ resolutionCache . set ( assetId , resolvedImage ) ;
138+ return resolvedImage ;
111139 }
112- return {
140+ const defaultImage = {
113141 image : {
114142 width : 960 ,
115143 height : 720 ,
116144 src : safeString ( asset ?. path , "" )
117145 } ,
118146 status : "provided-loaded"
119147 } ;
148+ resolutionCache . set ( assetId , defaultImage ) ;
149+ return defaultImage ;
120150 }
121151
122- const staticSource = runtimeAssetSources [ assetId ] ? cloneJson ( runtimeAssetSources [ assetId ] ) : null ;
152+ if ( ! staticSourceCache . has ( assetId ) ) {
153+ staticSourceCache . set ( assetId , runtimeAssetSources [ assetId ] ? cloneJson ( runtimeAssetSources [ assetId ] ) : null ) ;
154+ }
155+ const staticSource = staticSourceCache . get ( assetId ) ;
123156 const domain = getRuntimeBindingDomain ( assetId ) ;
124157 if ( ! domain ) {
158+ resolutionCache . set ( assetId , staticSource ) ;
125159 return staticSource ;
126160 }
127161
128- const runtimeRecord = resolveRuntimeAsset ( binding , { domain, assetId } ) ;
162+ const runtimeRecord = bindingIndex [ domain ] ?. get ( assetId ) || resolveRuntimeAsset ( binding , { domain, assetId } ) ;
129163 if ( ! runtimeRecord ) {
130164 appendAssetError ( errors , {
131165 code : "RUNTIME_LOOKUP_MISSING_BINDING" ,
@@ -134,7 +168,9 @@ export function createRuntimeManifestAssetLookup(options = {}) {
134168 assetId,
135169 message : `Missing runtime binding for ${ assetId } .`
136170 } ) ;
137- return missingBindingBehavior === "null" ? null : staticSource ;
171+ const missingResolution = missingBindingBehavior === "null" ? null : staticSource ;
172+ resolutionCache . set ( assetId , missingResolution ) ;
173+ return missingResolution ;
138174 }
139175
140176 const mergedSource = staticSource || { } ;
@@ -151,9 +187,11 @@ export function createRuntimeManifestAssetLookup(options = {}) {
151187 } ) ;
152188 if ( ! validation . valid ) {
153189 appendAssetErrors ( errors , validation . errors ) ;
190+ resolutionCache . set ( assetId , null ) ;
154191 return null ;
155192 }
156193
194+ resolutionCache . set ( assetId , mergedSource ) ;
157195 return mergedSource ;
158196 }
159197
@@ -175,9 +213,14 @@ export function createRuntimeManifestAssetLookup(options = {}) {
175213 Object . entries ( binding . domains || { } ) . map ( ( [ domain , entries ] ) => [ domain , Array . isArray ( entries ) ? entries . length : 0 ] )
176214 ) ,
177215 rejectedCount : Array . isArray ( binding . rejected ) ? binding . rejected . length : 0 ,
178- errorCount : errors . length
216+ errorCount : errors . length ,
217+ cache : {
218+ hits : cacheStats . hits ,
219+ misses : cacheStats . misses ,
220+ size : resolutionCache . size
221+ }
179222 } ,
180- manifest : cloneJson ( coordinatedManifest . manifest ) ,
223+ manifest : manifestSnapshot ,
181224 errors : errors . slice ( )
182225 } ;
183226 } ,
0 commit comments