11import { buildEndpointUrl } from '@datadog/js-core/transport'
2+ import { globalObject } from '@datadog/js-core/util'
23import { fetch } from '../../browser/fetch'
34import type { RumSdkConfig } from './remoteConfiguration.types'
45
@@ -15,6 +16,22 @@ export interface RemoteConfigurationEndpointOptions {
1516
1617export type FetchRemoteConfigurationResult = { ok : true ; value : RemoteConfiguration } | { ok : false ; error : Error }
1718
19+ // Typed interface for the global inflight fetch registry so deduplication
20+ // works across separate SDK bundles (e.g. RUM and Logs loaded as separate CDN
21+ // scripts on the same page) and in service-worker environments where `window`
22+ // is not available.
23+ interface GlobalWithInflightFetches {
24+ __ddRcInflight ?: Map < string , Promise < FetchRemoteConfigurationResult > >
25+ }
26+
27+ function getInflightFetches ( ) : Map < string , Promise < FetchRemoteConfigurationResult > > {
28+ const global = globalObject as GlobalWithInflightFetches
29+ if ( ! global . __ddRcInflight ) {
30+ global . __ddRcInflight = new Map ( )
31+ }
32+ return global . __ddRcInflight
33+ }
34+
1835export function getRemoteConfigurationId ( options : RemoteConfigurationEndpointOptions ) : string | undefined {
1936 return options . remoteConfiguration ?. id ?? options . remoteConfigurationId
2037}
@@ -31,30 +48,17 @@ export function buildEndpoint(options: RemoteConfigurationEndpointOptions): stri
3148 } )
3249}
3350
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-
4651export function fetchRemoteConfiguration (
4752 options : RemoteConfigurationEndpointOptions
4853) : Promise < FetchRemoteConfigurationResult > {
4954 const endpoint = buildEndpoint ( options )
5055 const inflightFetches = getInflightFetches ( )
5156
5257 if ( ! inflightFetches . has ( endpoint ) ) {
53- const win = window as unknown as Record < string , unknown >
5458 const promise = doFetchRemoteConfiguration ( endpoint ) . finally ( ( ) => {
5559 inflightFetches . delete ( endpoint )
5660 if ( inflightFetches . size === 0 ) {
57- delete win [ INFLIGHT_FETCHES_KEY ]
61+ delete ( globalObject as GlobalWithInflightFetches ) . __ddRcInflight
5862 }
5963 } )
6064 inflightFetches . set ( endpoint , promise )
0 commit comments