Skip to content

Commit 3dc17ac

Browse files
committed
fix: skip PostHog and Gleap init when key is an env.example placeholder
1 parent a242be5 commit 3dc17ac

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

apps/web/client/src/components/telemetry-provider.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ import { useEffect } from "react";
1414
// - Clears identities on user sign-out (see utils/telemetry/resetTelemetry).
1515
// - Keeps PostHog React context so existing `usePostHog()` calls continue to work.
1616

17+
// Reject empty values and `.env.example` placeholders (e.g. `<Your PostHog API key from ...>`),
18+
// which would otherwise cause 401/CORS console floods on init.
19+
const isConfigured = (value: string | undefined): value is string =>
20+
!!value && !value.startsWith("<");
21+
1722
let gleapSingleton: any | null = null;
1823

1924
export function TelemetryProvider({ children }: { children: React.ReactNode }) {
@@ -22,7 +27,7 @@ export function TelemetryProvider({ children }: { children: React.ReactNode }) {
2227

2328
// Initialize SDKs once
2429
useEffect(() => {
25-
if (env.NEXT_PUBLIC_POSTHOG_KEY) {
30+
if (isConfigured(env.NEXT_PUBLIC_POSTHOG_KEY)) {
2631
try {
2732
posthog.init(env.NEXT_PUBLIC_POSTHOG_KEY, {
2833
api_host: env.NEXT_PUBLIC_POSTHOG_HOST,
@@ -33,11 +38,9 @@ export function TelemetryProvider({ children }: { children: React.ReactNode }) {
3338
} catch (e) {
3439
console.warn("PostHog init failed", e);
3540
}
36-
} else {
37-
console.warn("PostHog key is not set, skipping initialization");
3841
}
3942

40-
if (env.NEXT_PUBLIC_GLEAP_API_KEY) {
43+
if (isConfigured(env.NEXT_PUBLIC_GLEAP_API_KEY)) {
4144
(async () => {
4245
try {
4346
// Dynamic import to avoid hard dependency when not installed
@@ -82,7 +85,7 @@ export function TelemetryProvider({ children }: { children: React.ReactNode }) {
8285
console.error("PostHog identify/reset error:", e);
8386
}
8487

85-
if (!env.NEXT_PUBLIC_GLEAP_API_KEY) return;
88+
if (!isConfigured(env.NEXT_PUBLIC_GLEAP_API_KEY)) return;
8689
(async () => {
8790
try {
8891
const Gleap = gleapSingleton ?? (await import("gleap")).default;
@@ -111,7 +114,7 @@ export function TelemetryProvider({ children }: { children: React.ReactNode }) {
111114

112115
// Soft re-initialize Gleap on path changes to guard against soft reloads/HMR
113116
useEffect(() => {
114-
if (!env.NEXT_PUBLIC_GLEAP_API_KEY) return;
117+
if (!isConfigured(env.NEXT_PUBLIC_GLEAP_API_KEY)) return;
115118
(async () => {
116119
try {
117120
const Gleap = gleapSingleton ?? (await import("gleap")).default;

apps/web/client/src/utils/analytics/server.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { env } from "@/env";
22
import { PostHog, type EventMessage } from "posthog-node";
33

4+
// Reject empty values and `.env.example` placeholders (e.g. `<Your PostHog API key from ...>`).
5+
const isConfigured = (value: string | undefined): value is string =>
6+
!!value && !value.startsWith("<");
7+
48
class PostHogSingleton {
59
private static instance: PostHog | null = null;
610
private constructor() { }
711

812
public static getInstance(): PostHog | null {
9-
if (!env.NEXT_PUBLIC_POSTHOG_KEY) {
10-
console.warn('PostHog key not found');
13+
if (!isConfigured(env.NEXT_PUBLIC_POSTHOG_KEY)) {
1114
return null;
1215
}
1316
if (!PostHogSingleton.instance) {

0 commit comments

Comments
 (0)