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 PostHog initialization when already loaded
Handle the case when posthog.__loaded is already true (e.g., during HMR or React StrictMode remounts). Previously, the init block was skipped but isInitialized was never set to true, preventing pageview tracking. Now we check posthog.__loaded first and set isInitialized immediately if PostHog is already initialized. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sushmithamallesh committed
Jan 12, 2026 at 17:21 UTC
a2bdc64da1f4e0ef91f7d658a3955b3b132b1e14
1 file changed
+8
-2
fumadocs/components/posthog-provider.tsx
+8
-2
@@ -9,14 +9,20 @@ export function PostHogProvider({ children }: { children: React.ReactNode }) {
9
const [isInitialized, setIsInitialized] = useState(false);
10
11
useEffect(() => {
12
- if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_POSTHOG_KEY && !posthog.__loaded) {
12
+ if (typeof window !== 'undefined' && process.env.NEXT_PUBLIC_POSTHOG_KEY) {
13
+ // Already loaded (e.g., HMR, StrictMode remount)
14
+ if (posthog.__loaded) {
15
+ setIsInitialized(true);
16
+ return;
17
+ }
18
+
19
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
20
api_host:
21
process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com',
22
person_profiles: 'identified_only',
23
capture_pageview: false, // We capture manually below
24
capture_pageleave: true,
19
- loaded: (posthog) => {
25
+ loaded: () => {
26
setIsInitialized(true);
27
},
28
});