-
Notifications
You must be signed in to change notification settings - Fork 207
feat(nuxt): Added feature flag composables #2579
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
Open
geferon
wants to merge
4
commits into
PostHog:main
Choose a base branch
from
geferon:feature/nuxt-composables
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+276
−1
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
31a3f26
feature(nuxt): Added feature flag composables
geferon 890df5e
fix(nuxt): Fixed SSR rendering error with `usePostHog`
geferon f9ce381
fix(nuxt): Fixed wrong method invocation in nuxt example
geferon fe183b8
fix(nuxt): Composables now dispose the listener correctly
geferon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <template> | ||
| <div style="padding: 20px; font-family: sans-serif"> | ||
| <h1>Feature Flags Testing Page</h1> | ||
| <p>Test the PostHog feature flag composables:</p> | ||
|
|
||
| <div style="margin-top: 20px"> | ||
| <h2>Feature Flag Examples</h2> | ||
|
|
||
| <div style="margin-top: 15px; padding: 15px; background-color: #f5f5f5; border-radius: 8px"> | ||
| <h3>1. Check if flag is enabled</h3> | ||
| <p><strong>Flag:</strong> beta-feature</p> | ||
| <p><strong>Enabled:</strong> {{ featureFlagEnabled ?? 'Loading...' }}</p> | ||
| </div> | ||
|
|
||
| <div style="margin-top: 15px; padding: 15px; background-color: #f5f5f5; border-radius: 8px"> | ||
| <h3>2. Get flag variant/value</h3> | ||
| <p><strong>Flag:</strong> test-variant</p> | ||
| <p><strong>Variant:</strong> {{ featureFlagVariant ?? 'Loading...' }}</p> | ||
| </div> | ||
|
|
||
| <div style="margin-top: 15px; padding: 15px; background-color: #f5f5f5; border-radius: 8px"> | ||
| <h3>3. Get flag payload</h3> | ||
| <p><strong>Flag:</strong> config-flag</p> | ||
| <p><strong>Payload:</strong> {{ payloadDisplay }}</p> | ||
| </div> | ||
|
|
||
| <div style="margin-top: 15px; padding: 15px; background-color: #f5f5f5; border-radius: 8px"> | ||
| <h3>4. Use PostHog directly</h3> | ||
| <button @click="captureEvent" style="padding: 10px; cursor: pointer; margin-right: 10px"> | ||
| Capture Event | ||
| </button> | ||
| <button @click="getAllFlags" style="padding: 10px; cursor: pointer">Get All Flags</button> | ||
| <p v-if="allFlags" style="margin-top: 10px"> | ||
| <strong>All Flags:</strong> | ||
| <pre>{{ JSON.stringify(allFlags, null, 2) }}</pre> | ||
| </p> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div style="margin-top: 20px"> | ||
| <a href="/" style="color: blue; text-decoration: underline">← Back to Error Testing</a> | ||
| </div> | ||
| </div> | ||
| </template> | ||
|
|
||
| <script setup lang="ts"> | ||
| import { computed, ref } from 'vue' | ||
|
|
||
| // Using the new composables | ||
| const featureFlagEnabled = useFeatureFlagEnabled('beta-feature') | ||
| const featureFlagVariant = useFeatureFlagVariantKey('test-variant') | ||
| const featureFlagPayload = useFeatureFlagPayload('config-flag') | ||
| const posthog = usePostHog() | ||
|
|
||
| const allFlags = ref<Record<string, boolean | string> | undefined>() | ||
|
|
||
| const payloadDisplay = computed(() => { | ||
| if (featureFlagPayload.value === undefined) { | ||
| return 'Loading...' | ||
| } | ||
| if (featureFlagPayload.value === null) { | ||
| return 'No payload' | ||
| } | ||
| return JSON.stringify(featureFlagPayload.value, null, 2) | ||
| }) | ||
|
|
||
| const captureEvent = () => { | ||
| posthog?.capture('feature_flags_test_event', { | ||
| page: 'feature-flags', | ||
| }) | ||
| alert('Event captured!') | ||
| } | ||
|
|
||
| const getAllFlags = () => { | ||
| allFlags.value = posthog?.featureFlags.getFlagVariants() | ||
| } | ||
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/nuxt/src/runtime/composables/useFeatureFlagEnabled.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { ref, onMounted } from 'vue' | ||
| import { usePostHog } from './usePostHog' | ||
|
|
||
| /** | ||
| * Check if a feature flag is enabled | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const isEnabled = useFeatureFlagEnabled('my-flag') | ||
| * ``` | ||
| * | ||
| * @param flag - The feature flag key | ||
| * @returns A reactive ref containing the feature flag enabled state (boolean | undefined) | ||
| */ | ||
| export function useFeatureFlagEnabled(flag: string) { | ||
| const posthog = usePostHog() | ||
| const featureEnabled = ref<boolean | undefined>(posthog?.isFeatureEnabled?.(flag)) | ||
|
|
||
| onMounted(() => { | ||
| if (!posthog) return | ||
|
|
||
| // Update when feature flags are loaded | ||
| const unsubscribe = posthog.onFeatureFlags?.(() => { | ||
| featureEnabled.value = posthog.isFeatureEnabled(flag) | ||
| }) | ||
|
|
||
| return () => { | ||
| if (unsubscribe) { | ||
| unsubscribe() | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| return featureEnabled | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
packages/nuxt/src/runtime/composables/useFeatureFlagPayload.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { ref, onMounted } from 'vue' | ||
| import { usePostHog } from './usePostHog' | ||
| import type { JsonType } from 'posthog-js' | ||
|
|
||
| /** | ||
| * Get the payload of a feature flag | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const payload = useFeatureFlagPayload('my-flag') | ||
| * ``` | ||
| * | ||
| * @param flag - The feature flag key | ||
| * @returns A reactive ref containing the feature flag payload | ||
| */ | ||
| export function useFeatureFlagPayload(flag: string) { | ||
| const posthog = usePostHog() | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const featureFlagPayload = ref<JsonType>(posthog?.getFeatureFlagPayload?.(flag)) | ||
|
|
||
| onMounted(() => { | ||
| if (!posthog) return | ||
|
|
||
| // Update when feature flags are loaded | ||
| const unsubscribe = posthog.onFeatureFlags?.(() => { | ||
| featureFlagPayload.value = posthog.getFeatureFlagPayload(flag) | ||
| }) | ||
|
|
||
| return () => { | ||
| if (unsubscribe) { | ||
| unsubscribe() | ||
| } | ||
| } | ||
| }) | ||
geferon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return featureFlagPayload | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
packages/nuxt/src/runtime/composables/useFeatureFlagVariantKey.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { ref, onMounted } from 'vue' | ||
| import { usePostHog } from './usePostHog' | ||
|
|
||
| /** | ||
| * Get the value/variant of a feature flag | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const variant = useFeatureFlagVariantKey('my-flag') | ||
| * ``` | ||
| * | ||
| * @param flag - The feature flag key | ||
| * @returns A reactive ref containing the feature flag value (string | boolean | undefined) | ||
| */ | ||
| export function useFeatureFlagVariantKey(flag: string) { | ||
| const posthog = usePostHog() | ||
| const featureFlagVariantKey = ref<string | boolean | undefined>(posthog?.getFeatureFlag?.(flag)) | ||
|
|
||
| onMounted(() => { | ||
| if (!posthog) return | ||
|
|
||
| // Update when feature flags are loaded | ||
| const unsubscribe = posthog.onFeatureFlags?.(() => { | ||
| featureFlagVariantKey.value = posthog.getFeatureFlag(flag) | ||
| }) | ||
|
|
||
| return () => { | ||
| if (unsubscribe) { | ||
| unsubscribe() | ||
| } | ||
| } | ||
| }) | ||
geferon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return featureFlagVariantKey | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { useNuxtApp } from '#app' | ||
| import type posthog from 'posthog-js' | ||
|
|
||
| /** | ||
| * Get the PostHog client instance | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const posthog = usePostHog() | ||
| * posthog.capture('event') | ||
| * ``` | ||
| * | ||
| * @returns The PostHog client instance | ||
| */ | ||
| export function usePostHog(): typeof posthog | undefined { | ||
| const { $posthog } = useNuxtApp() | ||
| return ($posthog as () => typeof posthog | undefined)?.() | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.