@@ -31,12 +31,42 @@ export function buildEndpoint(options: RemoteConfigurationEndpointOptions): stri
3131 } )
3232}
3333
34- export async function fetchRemoteConfiguration (
34+ // Use a window-level registry so deduplication works across separate SDK bundles
35+ // (e.g. RUM and Logs loaded as separate CDN scripts on the same page).
36+ const INFLIGHT_FETCHES_KEY = '__ddRcInflight'
37+
38+ function getInflightFetches ( ) : Map < string , Promise < FetchRemoteConfigurationResult > > {
39+ const win = window as unknown as Record < string , unknown >
40+ if ( ! win [ INFLIGHT_FETCHES_KEY ] ) {
41+ win [ INFLIGHT_FETCHES_KEY ] = new Map < string , Promise < FetchRemoteConfigurationResult > > ( )
42+ }
43+ return win [ INFLIGHT_FETCHES_KEY ] as Map < string , Promise < FetchRemoteConfigurationResult > >
44+ }
45+
46+ export function fetchRemoteConfiguration (
3547 options : RemoteConfigurationEndpointOptions
3648) : Promise < FetchRemoteConfigurationResult > {
49+ const endpoint = buildEndpoint ( options )
50+ const inflightFetches = getInflightFetches ( )
51+
52+ if ( ! inflightFetches . has ( endpoint ) ) {
53+ const win = window as unknown as Record < string , unknown >
54+ const promise = doFetchRemoteConfiguration ( endpoint ) . finally ( ( ) => {
55+ inflightFetches . delete ( endpoint )
56+ if ( inflightFetches . size === 0 ) {
57+ delete win [ INFLIGHT_FETCHES_KEY ]
58+ }
59+ } )
60+ inflightFetches . set ( endpoint , promise )
61+ }
62+
63+ return inflightFetches . get ( endpoint ) !
64+ }
65+
66+ async function doFetchRemoteConfiguration ( endpoint : string ) : Promise < FetchRemoteConfigurationResult > {
3767 let response : Response | undefined
3868 try {
39- response = await fetch ( buildEndpoint ( options ) )
69+ response = await fetch ( endpoint )
4070 } catch {
4171 response = undefined
4272 }
0 commit comments