Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Tbhesswebber opened this issue Jun 18, 2021 · 3 comments
Open

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

Tbhesswebber opened this issue Jun 18, 2021 · 3 comments

Comments

@Tbhesswebber
Copy link

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
Copy link

weyert commented Jun 23, 2021

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
Copy link
Contributor

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:
PostHog/posthog#4713
PostHog/posthog#4849
https://posthog.com/handbook/engineering/feature-flags-spec

@lharries
Copy link

lharries commented Mar 3, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants