Skip to content

Commit c0de3e8

Browse files
authored
Defer loading posthog (#128)
1 parent 1842d77 commit c0de3e8

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/App.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "@tanstack/react-router";
99
import { TanStackRouterDevtools } from "@tanstack/react-router-devtools";
1010
import posthog from "posthog-js";
11+
import { PropsWithChildren, useEffect } from "react";
1112
import { CookiesProvider } from "react-cookie";
1213
import "react-reflex/styles.css";
1314
import "typeface-roboto-mono/index.css"; // Import the Roboto Mono font.
@@ -55,12 +56,19 @@ const routeTree = rootRoute.addChildren([indexRoute, inlineRoute, embeddedRoute]
5556
const router = createRouter({ routeTree });
5657

5758
const config = AppConfig();
58-
if (config.posthog.apiKey && config.posthog.host) {
59-
posthog.init(config.posthog.apiKey, {
60-
api_host: config.posthog.host,
61-
person_profiles: "identified_only",
62-
defaults: "2025-11-30", // Enables automatic SPA pageview tracking via history API
63-
});
59+
60+
function PHProvider({ children }: PropsWithChildren) {
61+
useEffect(() => {
62+
if (config.posthog.apiKey && config.posthog.host) {
63+
posthog.init(config.posthog.apiKey, {
64+
api_host: config.posthog.host,
65+
person_profiles: "identified_only",
66+
defaults: "2025-11-30", // Enables automatic SPA pageview tracking via history API
67+
});
68+
}
69+
}, []);
70+
71+
return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
6472
}
6573

6674
function App() {
@@ -73,15 +81,15 @@ function App() {
7381
<Toaster />
7482
{/* @ts-ignore-error react-cookie's types are screwy; CI and (local and vercel) disagree about whether there's an error or not. */}
7583
<CookiesProvider>
76-
<PostHogProvider client={posthog}>
84+
<PHProvider>
7785
<ThemeProvider>
7886
<PlaygroundUIThemed {...PLAYGROUND_UI_COLORS} forceDarkMode={isEmbeddedPlayground}>
7987
<ConfirmDialogProvider>
8088
<RouterProvider router={router} />
8189
</ConfirmDialogProvider>
8290
</PlaygroundUIThemed>
8391
</ThemeProvider>
84-
</PostHogProvider>
92+
</PHProvider>
8593
</CookiesProvider>
8694
</>
8795
);

0 commit comments

Comments
 (0)