@posthog/nuxt 1.3.24 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -53,6 +53,67 @@ export default defineNuxtConfig({
53
53
  const { $posthog } = useNuxtApp()
54
54
  ```
55
55
 
56
+ ## Composables
57
+
58
+ The module provides auto-imported composables for working with feature flags:
59
+
60
+ ### `usePostHog()`
61
+
62
+ Returns the PostHog client instance.
63
+
64
+ ```vue
65
+ <script setup>
66
+ const posthog = usePostHog()
67
+
68
+ posthog.capture('event_name')
69
+ </script>
70
+ ```
71
+
72
+ ### `useFeatureFlagEnabled(flag: string)`
73
+
74
+ Returns a reactive ref that checks if a feature flag is enabled (returns `true`/`false`/`undefined`).
75
+
76
+ ```vue
77
+ <script setup>
78
+ const isEnabled = useFeatureFlagEnabled('my-flag')
79
+ </script>
80
+
81
+ <template>
82
+ <div v-if="isEnabled">Feature is enabled!</div>
83
+ </template>
84
+ ```
85
+
86
+ ### `useFeatureFlagVariantKey(flag: string)`
87
+
88
+ Returns a reactive ref containing the feature flag value/variant (returns the variant string, `true`/`false`, or `undefined`).
89
+
90
+ ```vue
91
+ <script setup>
92
+ const variant = useFeatureFlagVariantKey('my-flag')
93
+ </script>
94
+
95
+ <template>
96
+ <div v-if="variant === 'variant-a'">Show variant A</div>
97
+ <div v-else-if="variant === 'variant-b'">Show variant B</div>
98
+ </template>
99
+ ```
100
+
101
+ ### `useFeatureFlagPayload(flag: string)`
102
+
103
+ Returns a reactive ref containing the feature flag payload (returns any JSON value or `undefined`).
104
+
105
+ ```vue
106
+ <script setup>
107
+ const payload = useFeatureFlagPayload('config-flag')
108
+ </script>
109
+
110
+ <template>
111
+ <div v-if="payload">Config value: {{ payload.value }}</div>
112
+ </template>
113
+ ```
114
+
115
+ All these composables automatically update when feature flags are loaded or changed.
116
+
56
117
  4. On the server side, the PostHog client instance initialized by the plugin is intended exclusively for error tracking. If you require additional PostHog client functionality for other purposes, please instantiate a separate client within your application as needed.
57
118
 
58
119
  ## FAQ
package/dist/module.d.mts CHANGED
@@ -11,6 +11,7 @@ interface SourcemapsConfig {
11
11
  project?: string;
12
12
  logLevel?: LogLevel;
13
13
  deleteAfterUpload?: boolean;
14
+ batchSize?: number;
14
15
  }
15
16
  interface ModuleOptions {
16
17
  host: string;
package/dist/module.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "compatibility": {
5
5
  "nuxt": ">=3.7.0"
6
6
  },
7
- "version": "1.3.24",
7
+ "version": "1.4.0",
8
8
  "builder": {
9
9
  "@nuxt/module-builder": "1.0.2",
10
10
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { defineNuxtModule, createResolver, addPlugin, addServerPlugin } from '@nuxt/kit';
1
+ import { defineNuxtModule, createResolver, addPlugin, addServerPlugin, addImportsDir } from '@nuxt/kit';
2
2
  import { resolveBinaryPath, spawnLocal } from '@posthog/core/process';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { dirname } from 'node:path';
@@ -23,6 +23,7 @@ const module$1 = defineNuxtModule({
23
23
  const resolver = createResolver(import.meta.url);
24
24
  addPlugin(resolver.resolve("./runtime/vue-plugin"));
25
25
  addServerPlugin(resolver.resolve("./runtime/nitro-plugin"));
26
+ addImportsDir(resolver.resolve("./runtime/composables"));
26
27
  Object.assign(nuxt.options.runtimeConfig.public, {
27
28
  posthog: {
28
29
  publicKey: options.publicKey,
@@ -120,6 +121,9 @@ function getUploadArgs(directory, sourcemapsConfig) {
120
121
  if (sourcemapsConfig.deleteAfterUpload ?? true) {
121
122
  processOptions.push("--delete-after");
122
123
  }
124
+ if (sourcemapsConfig.batchSize) {
125
+ processOptions.push("--batch-size", sourcemapsConfig.batchSize.toString());
126
+ }
123
127
  return processOptions;
124
128
  }
125
129
 
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Check if a feature flag is enabled
3
+ *
4
+ * @remarks
5
+ * This composable initializes with the current feature flag value and automatically
6
+ * updates when PostHog feature flags are reloaded.
7
+ *
8
+ * **Server-Side Rendering (SSR) Behavior:**
9
+ * - During SSR, PostHog is typically not available or feature flags are not yet loaded
10
+ * - The returned ref will be `undefined` on the server side
11
+ * - The ref will be properly hydrated on the client side once PostHog initializes
12
+ * - Consider using a fallback value or `v-if` directive when rendering based on this value
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const isEnabled = useFeatureFlagEnabled('my-flag')
17
+ * ```
18
+ *
19
+ * @param flag - The feature flag key
20
+ * @returns A reactive ref containing the feature flag enabled state (boolean | undefined)
21
+ */
22
+ export declare function useFeatureFlagEnabled(flag: string): import("vue").Ref<boolean | undefined, boolean | undefined>;
@@ -0,0 +1,18 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { usePostHog } from "./usePostHog.js";
3
+ export function useFeatureFlagEnabled(flag) {
4
+ const posthog = usePostHog();
5
+ const featureEnabled = ref(posthog?.isFeatureEnabled?.(flag));
6
+ let unsubscribe;
7
+ onMounted(() => {
8
+ if (!posthog) return;
9
+ featureEnabled.value = posthog.isFeatureEnabled(flag);
10
+ unsubscribe = posthog.onFeatureFlags?.(() => {
11
+ featureEnabled.value = posthog.isFeatureEnabled(flag);
12
+ });
13
+ });
14
+ onUnmounted(() => {
15
+ unsubscribe?.();
16
+ });
17
+ return featureEnabled;
18
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Get the payload of a feature flag
3
+ *
4
+ * @remarks
5
+ * This composable initializes with the current feature flag payload and automatically
6
+ * updates when PostHog feature flags are reloaded.
7
+ *
8
+ * **Server-Side Rendering (SSR) Behavior:**
9
+ * - During SSR, PostHog is typically not available or feature flags are not yet loaded
10
+ * - The returned ref will be `undefined` on the server side
11
+ * - The ref will be properly hydrated on the client side once PostHog initializes
12
+ * - Consider using a fallback value or `v-if` directive when rendering based on this value
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const payload = useFeatureFlagPayload('my-flag')
17
+ * ```
18
+ *
19
+ * @param flag - The feature flag key
20
+ * @returns A reactive ref containing the feature flag payload
21
+ */
22
+ export declare function useFeatureFlagPayload(flag: string): import("vue").Ref<any, any>;
@@ -0,0 +1,18 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { usePostHog } from "./usePostHog.js";
3
+ export function useFeatureFlagPayload(flag) {
4
+ const posthog = usePostHog();
5
+ const featureFlagPayload = ref(posthog?.getFeatureFlagPayload?.(flag));
6
+ let unsubscribe;
7
+ onMounted(() => {
8
+ if (!posthog) return;
9
+ featureFlagPayload.value = posthog.getFeatureFlagPayload(flag);
10
+ unsubscribe = posthog.onFeatureFlags?.(() => {
11
+ featureFlagPayload.value = posthog.getFeatureFlagPayload(flag);
12
+ });
13
+ });
14
+ onUnmounted(() => {
15
+ unsubscribe?.();
16
+ });
17
+ return featureFlagPayload;
18
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Get the value/variant of a feature flag
3
+ *
4
+ * @remarks
5
+ * This composable initializes with the current feature flag variant key and automatically
6
+ * updates when PostHog feature flags are reloaded.
7
+ *
8
+ * **Server-Side Rendering (SSR) Behavior:**
9
+ * - During SSR, PostHog is typically not available or feature flags are not yet loaded
10
+ * - The returned ref will be `undefined` on the server side
11
+ * - The ref will be properly hydrated on the client side once PostHog initializes
12
+ * - Consider using a fallback value or `v-if` directive when rendering based on this value
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const variant = useFeatureFlagVariantKey('my-flag')
17
+ * ```
18
+ *
19
+ * @param flag - The feature flag key
20
+ * @returns A reactive ref containing the feature flag value (string | boolean | undefined)
21
+ */
22
+ export declare function useFeatureFlagVariantKey(flag: string): import("vue").Ref<string | boolean | undefined, string | boolean | undefined>;
@@ -0,0 +1,18 @@
1
+ import { ref, onMounted, onUnmounted } from "vue";
2
+ import { usePostHog } from "./usePostHog.js";
3
+ export function useFeatureFlagVariantKey(flag) {
4
+ const posthog = usePostHog();
5
+ const featureFlagVariantKey = ref(posthog?.getFeatureFlag?.(flag));
6
+ let unsubscribe;
7
+ onMounted(() => {
8
+ if (!posthog) return;
9
+ featureFlagVariantKey.value = posthog.getFeatureFlag(flag);
10
+ unsubscribe = posthog.onFeatureFlags?.(() => {
11
+ featureFlagVariantKey.value = posthog.getFeatureFlag(flag);
12
+ });
13
+ });
14
+ onUnmounted(() => {
15
+ unsubscribe?.();
16
+ });
17
+ return featureFlagVariantKey;
18
+ }
@@ -0,0 +1,17 @@
1
+ import type posthog from 'posthog-js';
2
+ /**
3
+ * Get the PostHog client instance
4
+ *
5
+ * @remarks
6
+ * This composable provides access to the PostHog client instance initialized.
7
+ * It returns `undefined` on the server side or if PostHog is not yet initialized.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * const posthog = usePostHog()
12
+ * posthog.capture('event')
13
+ * ```
14
+ *
15
+ * @returns The PostHog client instance
16
+ */
17
+ export declare function usePostHog(): typeof posthog | undefined;
@@ -0,0 +1,5 @@
1
+ import { useNuxtApp } from "#app";
2
+ export function usePostHog() {
3
+ const { $posthog } = useNuxtApp();
4
+ return $posthog?.();
5
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/nuxt",
3
- "version": "1.3.24",
3
+ "version": "1.4.0",
4
4
  "description": "Nuxt module for Posthog 🦔",
5
5
  "repository": {
6
6
  "type": "git",
@@ -20,11 +20,11 @@
20
20
  "dist"
21
21
  ],
22
22
  "dependencies": {
23
- "@posthog/cli": "~0.5.17",
23
+ "@posthog/cli": "~0.5.20",
24
24
  "@nuxt/kit": ">=3.7.0",
25
- "posthog-node": "5.17.3",
26
- "@posthog/core": "1.8.0",
27
- "posthog-js": "1.309.0"
25
+ "posthog-node": "5.18.0",
26
+ "@posthog/core": "1.9.0",
27
+ "posthog-js": "1.310.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/node": "^20.0.0",