@unproducts/nuxt-posthog 2.0.4 → 2.0.6

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/dist/module.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
2
  import { PostHogConfig } from 'posthog-js';
3
+ import { PostHogOptions } from 'posthog-node';
3
4
 
4
5
  interface ModuleOptions {
5
6
  /**
@@ -42,21 +43,23 @@ interface ModuleOptions {
42
43
  */
43
44
  clientOptions?: Partial<PostHogConfig>;
44
45
  /**
45
- * If set to true, the module will be disabled (no events will be sent to PostHog).
46
- * This is useful for development environments. Directives and components will still be available for you to use.
47
- * @default false
48
- * @type boolean
46
+ * PostHog Server options
47
+ * @default {
48
+ * api_host: process.env.POSTHOG_API_HOST,
49
+ * }
50
+ * @type object
51
+ * @docs https://posthog.com/docs/libraries/node#config
49
52
  */
50
- disabled?: boolean;
53
+ serverOptions?: Partial<PostHogOptions>;
51
54
  /**
52
55
  * If set to true, the module will be enabled on the client side.
53
- * @default true
56
+ * @default false
54
57
  * @type boolean
55
58
  */
56
59
  client?: boolean;
57
60
  /**
58
61
  * If set to true, the module will be enabled on the server side.
59
- * @default true
62
+ * @default false
60
63
  * @type boolean
61
64
  */
62
65
  server?: boolean;
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-posthog",
3
3
  "configKey": "posthog",
4
- "version": "2.0.4",
4
+ "version": "2.0.6",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -11,35 +11,35 @@ const module$1 = defineNuxtModule({
11
11
  host: process.env.POSTHOG_API_HOST,
12
12
  capturePageViews: true,
13
13
  capturePageLeaves: true,
14
- disabled: false,
15
- client: true,
16
- server: true,
14
+ client: false,
15
+ server: false,
17
16
  proxy: false
18
17
  },
19
18
  setup(options, nuxt) {
20
19
  const { resolve } = createResolver(import.meta.url);
21
- if (!options.key || !options.host) {
22
- options.disabled = true;
23
- } else if (!options.disabled) {
24
- nuxt.options.runtimeConfig.public.posthog = defu(
25
- nuxt.options.runtimeConfig.public.posthog,
26
- {
27
- key: options.key,
28
- host: options.host,
29
- capturePageViews: options.capturePageViews,
30
- capturePageLeaves: options.capturePageLeaves,
31
- clientOptions: options.clientOptions,
32
- proxy: options.proxy
33
- }
34
- );
35
- nuxt.options.runtimeConfig.posthog = defu(nuxt.options.runtimeConfig.posthog, {
36
- client: !!options.client,
37
- server: !!options.server
38
- });
20
+ const mergedKeyHost = defu(nuxt.options.runtimeConfig.public.posthog, { key: options.key, host: options.host });
21
+ if (options.server) {
22
+ nuxt.options.runtimeConfig.posthog = {
23
+ ...nuxt.options.runtimeConfig.posthog,
24
+ server: true,
25
+ ...options.serverOptions && { serverOptions: options.serverOptions }
26
+ };
27
+ }
28
+ if (options.client) {
29
+ nuxt.options.runtimeConfig.public.posthog = {
30
+ ...nuxt.options.runtimeConfig.public.posthog,
31
+ key: mergedKeyHost.key,
32
+ host: mergedKeyHost.host,
33
+ capturePageViews: options.capturePageViews,
34
+ capturePageLeaves: options.capturePageLeaves,
35
+ clientOptions: options.clientOptions,
36
+ proxy: options.proxy,
37
+ client: true
38
+ };
39
39
  }
40
- const config = nuxt.options.runtimeConfig.public.posthog;
41
- if (config && config.proxy) {
42
- const url = new URL(config.host);
40
+ const publicPosthog = nuxt.options.runtimeConfig.public.posthog;
41
+ if (publicPosthog?.proxy && publicPosthog?.host) {
42
+ const url = new URL(publicPosthog.host);
43
43
  const region = url.hostname.split(".")[0];
44
44
  if (!region) {
45
45
  throw new Error("Invalid PostHog API host when setting proxy");
@@ -1,2 +1,2 @@
1
- declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
1
+ declare const _default: import("#app").Plugin<Record<string, unknown>> & import("#app").ObjectPlugin<Record<string, unknown>>;
2
2
  export default _default;
@@ -4,8 +4,7 @@ import { getCookie } from "h3";
4
4
  export default defineNitroPlugin((nitroApp) => {
5
5
  nitroApp.hooks.hook("request", async (event) => {
6
6
  const config = useRuntimeConfig().public.posthog;
7
- const runtimeConfig = useRuntimeConfig().posthog;
8
- if (!config || !runtimeConfig.server) return;
7
+ if (!config?.client) return;
9
8
  const distinctId = getCookie(event, "ph-identify");
10
9
  event.context.posthogId = distinctId;
11
10
  });
@@ -1,8 +1,8 @@
1
1
  import type { PostHog } from 'posthog-js';
2
- declare const _default: import("nuxt/app").Plugin<{
2
+ declare const _default: import("#app").Plugin<{
3
3
  posthog: "Deprecated: use $clientPosthog instead.";
4
4
  clientPosthog: PostHog | null;
5
- }> & import("nuxt/app").ObjectPlugin<{
5
+ }> & import("#app").ObjectPlugin<{
6
6
  posthog: "Deprecated: use $clientPosthog instead.";
7
7
  clientPosthog: PostHog | null;
8
8
  }>;
@@ -2,18 +2,21 @@ import { defineNuxtPlugin, useCookie, useRouter, useRuntimeConfig, useState } fr
2
2
  import { defu } from "defu";
3
3
  export default defineNuxtPlugin({
4
4
  name: "posthog",
5
- enforce: "pre",
5
+ enforce: "post",
6
6
  setup: async () => {
7
- console.log("setup posthog client");
8
- const runtimeConfig = useRuntimeConfig().posthog;
9
7
  const config = useRuntimeConfig().public.posthog;
10
- if (!config || !runtimeConfig.client)
8
+ if (!config?.client)
11
9
  return {
12
10
  provide: {
13
11
  posthog: "Deprecated: use $clientPosthog instead.",
14
12
  clientPosthog: null
15
13
  }
16
14
  };
15
+ if (!config.key || !config.host) {
16
+ throw new Error(
17
+ "PostHog client is enabled but key or host not found. Set client to false in module options to disable."
18
+ );
19
+ }
17
20
  const posthog = (await import("posthog-js")).posthog;
18
21
  const posthogFeatureFlags = useState("ph-feature-flags");
19
22
  const posthogFeatureFlagPayloads = useState("ph-feature-flag-payloads");
@@ -1,7 +1,7 @@
1
1
  import type { PostHog } from 'posthog-node';
2
- declare const _default: import("nuxt/app").Plugin<{
2
+ declare const _default: import("#app").Plugin<{
3
3
  serverPosthog: PostHog | null;
4
- }> & import("nuxt/app").ObjectPlugin<{
4
+ }> & import("#app").ObjectPlugin<{
5
5
  serverPosthog: PostHog | null;
6
6
  }>;
7
7
  export default _default;
@@ -1,18 +1,22 @@
1
1
  import { defineNuxtPlugin, useCookie, useRuntimeConfig, useState } from "#app";
2
2
  export default defineNuxtPlugin({
3
3
  name: "posthog-server",
4
- enforce: "pre",
4
+ enforce: "post",
5
5
  setup: async () => {
6
- const config = useRuntimeConfig().public.posthog;
7
- const runtimeConfig = useRuntimeConfig().posthog;
8
- if (!config || !runtimeConfig.server)
6
+ const publicConfig = useRuntimeConfig().public.posthog;
7
+ if (!publicConfig?.client)
9
8
  return {
10
9
  provide: {
11
10
  serverPosthog: null
12
11
  }
13
12
  };
13
+ if (!publicConfig.key || !publicConfig.host) {
14
+ throw new Error(
15
+ "PostHog client (SSR) is enabled but key or host not found. Set client to false in module options to disable."
16
+ );
17
+ }
14
18
  const PostHog = (await import("posthog-node")).PostHog;
15
- const posthog = new PostHog(config.key, { host: config.host });
19
+ const posthog = new PostHog(publicConfig.key, { host: publicConfig.host });
16
20
  await posthog.reloadFeatureFlags();
17
21
  const identity = useCookie("ph-identify", { default: () => "" });
18
22
  const { featureFlags, featureFlagPayloads } = await posthog.getAllFlagsAndPayloads(identity.value);
@@ -1,13 +1,20 @@
1
1
  import type { ModuleOptions } from '../../module';
2
2
 
3
3
  declare module '@nuxt/schema' {
4
+ /** Server-only config (private). Key/host live in public only; Nitro usePostHog reads serverOptions here. */
4
5
  interface RuntimeConfig {
5
- posthog: Pick<ModuleOptions, 'client' | 'server'>;
6
+ posthog: {
7
+ server: boolean;
8
+ serverOptions?: ModuleOptions['serverOptions'];
9
+ };
6
10
  }
11
+ /** Client config (public). Single source of truth for key/host. Read in client plugins and server plugin. */
7
12
  interface PublicRuntimeConfig {
8
13
  posthog?: Pick<
9
14
  ModuleOptions,
10
15
  'key' | 'host' | 'capturePageViews' | 'capturePageLeaves' | 'clientOptions' | 'proxy'
11
- >;
16
+ > & {
17
+ client: boolean;
18
+ };
12
19
  }
13
20
  }
@@ -1,2 +1,2 @@
1
1
  import type { PostHog } from 'posthog-node';
2
- export declare const usePostHog: () => Promise<PostHog | undefined>;
2
+ export declare const usePostHog: () => Promise<PostHog | null>;
@@ -1,12 +1,19 @@
1
1
  import { useRuntimeConfig } from "#imports";
2
+ const POSTHOG_SERVER_MISSING = "PostHog server is enabled but key or host not found. Set server to false in module options to disable.";
2
3
  let posthog = null;
3
4
  export const usePostHog = async () => {
4
- const config = useRuntimeConfig().public.posthog;
5
- const runtimeConfig = useRuntimeConfig().posthog;
6
- if (!config || !runtimeConfig.server) return;
5
+ const serverConfig = useRuntimeConfig().posthog;
6
+ const publicConfig = useRuntimeConfig().public.posthog;
7
+ if (!serverConfig?.server) return null;
8
+ if (!publicConfig?.key || !publicConfig?.host) {
9
+ throw new Error(POSTHOG_SERVER_MISSING);
10
+ }
7
11
  if (!posthog) {
8
12
  const PostHog = (await import("posthog-node")).PostHog;
9
- posthog = new PostHog(config.key, { host: config.host });
13
+ posthog = new PostHog(publicConfig.key, {
14
+ host: publicConfig.host,
15
+ ...serverConfig.serverOptions
16
+ });
10
17
  }
11
18
  return posthog;
12
19
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unproducts/nuxt-posthog",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "description": "PostHog module for nuxt",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "peerDependencies": {
41
41
  "posthog-js": "^1.252.0",
42
- "posthog-node": "5.14.0"
42
+ "posthog-node": "^5.14.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@nuxt/devtools": "^3.1.1",
@@ -48,6 +48,7 @@
48
48
  "@nuxt/schema": "^4.2.1",
49
49
  "@nuxt/test-utils": "^3.19.1",
50
50
  "@types/node": "^24.0.1",
51
+ "baseline-browser-mapping": "^2.9.19",
51
52
  "changelogen": "^0.6.1",
52
53
  "eslint": "^9.29.0",
53
54
  "eslint-config-prettier": "^10.1.5",