Skip to content

[bug] Suppress PostHog console errors during local development #2707

@reddygtvs

Description

@reddygtvs

Describe the bug

Image

As a new user with the default local development setup, while working on issues these PostHog console errors were deeply frustrating. The console gets flooded with 401s and CORS failures intermittently, making it harder to differentiate between actual errors and config errors.

The issue happens because PostHog tries to initialize with the placeholder API key from .env: <Your PostHog API key from https://posthog.com/docs/libraries/next-js>. The current code only checks if the key exists, not if it's valid:

if (!env.NEXT_PUBLIC_POSTHOG_KEY) {
    return;
}
// Still initializes with placeholder value!
posthog.init(env.NEXT_PUBLIC_POSTHOG_KEY, {...})

I've been using a modified version locally to suppress these errors and refined it to be more robust. Created a ConditionalPostHogProvider that semi-validates the key format before initializing:

const hasValidKey = env.NEXT_PUBLIC_POSTHOG_KEY && 
                   !env.NEXT_PUBLIC_POSTHOG_KEY.includes('<Your PostHog API key') &&
                   !env.NEXT_PUBLIC_POSTHOG_KEY.startsWith('<')

Takes a more defensive approach than other services, preventing initialization entirely when keys contain placeholder values, as this is optional. This eliminates console noise when PostHog isn't configured, while staying backward compatible.

Should make local development setup much smoother when PostHog is not needed.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions