Skip to content

Commit 4561428

Browse files
committed
fix(rudderstack): proper timeout option
1 parent d521b74 commit 4561428

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

.changeset/sour-comics-flash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@scaleway/use-analytics": patch
3+
---
4+
5+
Add a proper timeout option to avoid re-render

packages/use-analytics/src/analytics/useAnalytics.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import type { ReactNode } from 'react'
55
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
66
import { destSDKBaseURL, pluginsSDKBaseURL } from '../constants'
77
import type { CategoryKind } from '../types'
8-
import { defaultConsentOptions, defaultLoadOptions, defaultTimeout } from './constants'
8+
import {
9+
defaultConsentOptions,
10+
defaultLoadOptions,
11+
defaultTimeout,
12+
} from './constants'
913
import { normalizeIdsMigration } from './normalizeIdsMigration'
1014

1115
type Analytics = RudderAnalytics
@@ -92,17 +96,25 @@ export function AnalyticsProvider<T extends Events>({
9296
// 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.
9397
useEffect(() => {
9498
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)
98111
}
99112

100113
return () => {
101114
clearTimeout(timer)
102115
}
103116
}, [
104117
isAnalyticsReady,
105-
internalAnalytics,
106118
setIsAnalyticsReady,
107119
shouldRenderOnlyWhenReady,
108120
timeout,
@@ -144,7 +156,13 @@ export function AnalyticsProvider<T extends Events>({
144156
})
145157

146158
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+
*/
148166
setIsAnalyticsReady(true)
149167
})
150168
}

pnpm-lock.yaml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ catalog:
2020
'@formatjs/fast-memoize': 2.2.7
2121
'@formatjs/icu-messageformat-parser': 2.11.2
2222
'@growthbook/growthbook-react': 1.6.1
23-
'@rudderstack/analytics-js': 3.22.0
23+
'@rudderstack/analytics-js': 3.24.0
2424
'@scaleway/use-segment': 1.1.11
2525
'@segment/analytics-next': 1.81.1
2626
'@stylistic/eslint-plugin': 4.4.1

0 commit comments

Comments
 (0)