Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
fix(fumadocs): fix PostHog initialization race condition
- Add isInitialized state to track when PostHog is ready - Check posthog.__loaded to prevent double initialization - Only render pageview tracker after PostHog is loaded Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sushmithamallesh committed
Jan 12, 2026 at 17:09 UTC
145a249e1a7177d60861d4e786b6caade8e94844
1 file changed
+8
-3
fumadocs/components/posthog-provider.tsx
+8
-3
@@ -2,18 +2,23 @@
2
3
import posthog from 'posthog-js';
4
import { PostHogProvider as PHProvider, usePostHog } from 'posthog-js/react';
5
-import { Suspense, useEffect } from 'react';
5
+import { Suspense, useEffect, useState } from 'react';
6
import { usePathname, useSearchParams } from 'next/navigation';
7
8
export function PostHogProvider({ children }: { children: React.ReactNode }) {
9
+ const [isInitialized, setIsInitialized] = useState(false);
10
+
11
useEffect(() => {
10
- if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_POSTHOG_KEY) {
12
+ if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_POSTHOG_KEY && !posthog.__loaded) {
13
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
14
api_host:
15
process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
16
person_profiles: 'identified_only',
17
capture_pageview: false, // We capture manually below
18
capture_pageleave: true,
19
+ loaded: (posthog) => {
20
+ setIsInitialized(true);
21
+ },
22
});
23
}
24
}, []);
@@ -24,7 +29,7 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
29
30
return (
31
<PHProvider client={posthog}>
27
- <SuspendedPostHogPageView />
32
+ {isInitialized && <SuspendedPostHogPageView />}
33
{children}
34
</PHProvider>
35
);