Skip to content

postHogAdapter expects NEXT_PUBLIC_POSTHOG_KEY but Vercel integration provides NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN #365

@git-johnson

Description

@git-johnson

The default postHogAdapter singleton in @flags-sdk/posthog calls assertEnv("NEXT_PUBLIC_POSTHOG_KEY")

postHogKey: assertEnv('NEXT_PUBLIC_POSTHOG_KEY'),
, but the Vercel PostHog integration provisions NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN. This means installing both the integration and using the Flags SDK in your project doesn't produce a working setup. Flags silently fail to evaluate.

I was able to workaround this using the createPostHogAdapter explicitly instead of letting the default singleton do its job.

import { createPostHogAdapter } from "@flags-sdk/posthog";                                 
                                                                                           
  const postHogAdapter = createPostHogAdapter({                                              
    postHogKey: process.env.NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN!,
    postHogOptions: {                                                                        
      host: process.env.NEXT_PUBLIC_POSTHOG_HOST ?? "https://us.posthog.com",              
    },                                                                                       
  });

I suggest the SDK try NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN when NEXT_PUBLIC_POSTHOG_KEY is not set as it would provide the most seamless experience for implementers. However, this does make assumptions that the PostHog integration won't change on you in the future.

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions