PostHog / posthog-node

Official PostHog Node library
MIT License
21 stars 19 forks source link

Question: Feature Flags in Server-Side Next.js #39

Open Tbhesswebber opened 3 years ago

Tbhesswebber commented 3 years ago

Context

Next.js (and many other frameworks) provide methods for server-side rendering/hydrating of React components. In Next, this is achieved by exporting a function called getServerSideProps from the same file as your page component. getServerSideProps can have access to the logged-in user and allows redirects, including out-of-the-box support for 404 pages.

For pages that aren't yet available to the public, I'd like to use a feature flag and the user identity to return the correct props or redirect.

The Problem

It seems that the current posthog-node implementation requires a personal API key with full account access rather than an organizational API key with scoped account access. This seems to imply that API keys are intended to exist for dev/analytics tools for team members and not for production application use.

The Question

Does instantiating posthog-node on the server with a personal API key provide any additional access to the API through the posthog instance?

weyert commented 3 years ago

I have been extracting it from the cookie for my next.js api routes.

I have been thinking of somehow generating the distinct id on the server-side and pass it to posthog-node for anonymous users together with the active feature flags for this anonymous user and then when the user is authenticated it set the id to the user id and reload the feature flags on the client-side

yakkomajuri commented 3 years ago

Hey! Personal API keys are used to improve the performance of flag calculations, by ensuring flags with no filters calculate without having to hit PostHog for a response. It is suboptimal to use a Personal API Key, and there's a discussion happening to figure out the approach going forward.

See: https://github.com/PostHog/posthog/issues/4713 https://github.com/PostHog/posthog/issues/4849 https://posthog.com/handbook/engineering/feature-flags-spec

lharries commented 1 year ago

We now our Next.js guide now talks about how to use server side feature flags: https://posthog.com/docs/integrate/third-party/next-js