Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-trains-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scaleway/use-analytics": patch
---

Add timeout option also when shouldRenderOnlyWhenReady is not set
2 changes: 2 additions & 0 deletions packages/use-analytics/src/analytics/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ConsentOptions, LoadOptions } from '@rudderstack/analytics-js'

export const defaultTimeout = 5000

export const defaultConsentOptions: ConsentOptions = {
trackConsent: false,
/**
Expand Down
10 changes: 4 additions & 6 deletions packages/use-analytics/src/analytics/useAnalytics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ReactNode } from 'react'
import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
import { destSDKBaseURL, pluginsSDKBaseURL } from '../constants'
import type { CategoryKind } from '../types'
import { defaultConsentOptions, defaultLoadOptions } from './constants'
import { defaultConsentOptions, defaultLoadOptions, defaultTimeout } from './constants'
import { normalizeIdsMigration } from './normalizeIdsMigration'

type Analytics = RudderAnalytics
Expand Down Expand Up @@ -50,7 +50,7 @@ export type AnalyticsProviderProps<T> = {
loadOptions?: LoadOptions

/**
* This option force provider to render children only when isAnalytics is ready
* 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.
*/
shouldRenderOnlyWhenReady?: boolean
/**
Expand Down Expand Up @@ -92,11 +92,9 @@ export function AnalyticsProvider<T extends Events>({
// 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.
useEffect(() => {
let timer: ReturnType<typeof setTimeout> | undefined
if (!isAnalyticsReady && timeout) {
if (shouldRenderOnlyWhenReady) {
timer = setTimeout(() => setIsAnalyticsReady(true), timeout)
if (!isAnalyticsReady && (Number.isFinite(timeout) || shouldRenderOnlyWhenReady)) {
timer = setTimeout(() => setIsAnalyticsReady(true), timeout ?? defaultTimeout)
onError?.(new Error('Analytics Setup Timeout'))
}
}

return () => {
Expand Down
Loading