@@ -5,7 +5,11 @@ import type { ReactNode } from 'react'
5
5
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
6
6
import { destSDKBaseURL , pluginsSDKBaseURL } from '../constants'
7
7
import type { CategoryKind } from '../types'
8
- import { defaultConsentOptions , defaultLoadOptions , defaultTimeout } from './constants'
8
+ import {
9
+ defaultConsentOptions ,
10
+ defaultLoadOptions ,
11
+ defaultTimeout ,
12
+ } from './constants'
9
13
import { normalizeIdsMigration } from './normalizeIdsMigration'
10
14
11
15
type Analytics = RudderAnalytics
@@ -92,17 +96,25 @@ export function AnalyticsProvider<T extends Events>({
92
96
// This effect will unlock the case where we have a failure with the load of the analytics.load as rudderstack doesn't provider any solution for this case.
93
97
useEffect ( ( ) => {
94
98
let timer : ReturnType < typeof setTimeout > | undefined
95
- if ( ! isAnalyticsReady && ( Number . isFinite ( timeout ) || shouldRenderOnlyWhenReady ) ) {
96
- timer = setTimeout ( ( ) => setIsAnalyticsReady ( true ) , timeout ?? defaultTimeout )
97
- onError ?.( new Error ( 'Analytics Setup Timeout' ) )
99
+ if (
100
+ ! isAnalyticsReady &&
101
+ ( Number . isFinite ( timeout ) || shouldRenderOnlyWhenReady )
102
+ ) {
103
+ timer = setTimeout ( ( ) => {
104
+ setIsAnalyticsReady ( true )
105
+ onError ?.( new Error ( 'Timeout' ) )
106
+ } , timeout ?? defaultTimeout )
107
+ }
108
+
109
+ if ( isAnalyticsReady ) {
110
+ clearTimeout ( timer )
98
111
}
99
112
100
113
return ( ) => {
101
114
clearTimeout ( timer )
102
115
}
103
116
} , [
104
117
isAnalyticsReady ,
105
- internalAnalytics ,
106
118
setIsAnalyticsReady ,
107
119
shouldRenderOnlyWhenReady ,
108
120
timeout ,
@@ -144,7 +156,13 @@ export function AnalyticsProvider<T extends Events>({
144
156
} )
145
157
146
158
analytics . ready ( ( ) => {
147
- setAnalytics ( analytics )
159
+ /**
160
+ * this will wait for client destination to be ready, but analytics is ready.
161
+ * we can listen for RSA_Ready event to know when the analytics is ready if we don't want to wait for the client destination to be ready.
162
+ * document.addEventListener('RSA_Ready', function(e) {
163
+ * console.log('RSA_Ready', e.detail.analyticsInstance);
164
+ * });
165
+ */
148
166
setIsAnalyticsReady ( true )
149
167
} )
150
168
}
0 commit comments