Skip to content

Commit 2a10859

Browse files
chore(deps): update dependency wait-for-expect to v4 (#2779) (#2783)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
1 parent 06865aa commit 2a10859

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

.changeset/chilly-trains-fix.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 timeout option also when shouldRenderOnlyWhenReady is not set

packages/use-analytics/src/analytics/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { ConsentOptions, LoadOptions } from '@rudderstack/analytics-js'
22

3+
export const defaultTimeout = 5000
4+
35
export const defaultConsentOptions: ConsentOptions = {
46
trackConsent: false,
57
/**

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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 } from './constants'
8+
import { defaultConsentOptions, defaultLoadOptions, defaultTimeout } from './constants'
99
import { normalizeIdsMigration } from './normalizeIdsMigration'
1010

1111
type Analytics = RudderAnalytics
@@ -50,7 +50,7 @@ export type AnalyticsProviderProps<T> = {
5050
loadOptions?: LoadOptions
5151

5252
/**
53-
* This option force provider to render children only when isAnalytics is ready
53+
* This option force provider to render children only when isAnalytics is ready, you can also set a timeout to prevent blocking indefinitely and use isAnalyticsReady to show a loading screen.
5454
*/
5555
shouldRenderOnlyWhenReady?: boolean
5656
/**
@@ -92,11 +92,9 @@ export function AnalyticsProvider<T extends Events>({
9292
// 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.
9393
useEffect(() => {
9494
let timer: ReturnType<typeof setTimeout> | undefined
95-
if (!isAnalyticsReady && timeout) {
96-
if (shouldRenderOnlyWhenReady) {
97-
timer = setTimeout(() => setIsAnalyticsReady(true), timeout)
95+
if (!isAnalyticsReady && (Number.isFinite(timeout) || shouldRenderOnlyWhenReady)) {
96+
timer = setTimeout(() => setIsAnalyticsReady(true), timeout ?? defaultTimeout)
9897
onError?.(new Error('Analytics Setup Timeout'))
99-
}
10098
}
10199

102100
return () => {

0 commit comments

Comments
 (0)