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

feat: proxy #74

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export interface ModuleOptions {
* @type boolean
*/
disabled?: boolean;

/**
* If set to true, PostHog will be proxied through the Nuxt server.
* @default false
* @type boolean
* @docs https://posthog.com/docs/advanced/proxy/nuxt
*/
proxy?: boolean;
}

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -67,6 +75,7 @@ export default defineNuxtModule<ModuleOptions>({
capturePageViews: true,
capturePageLeaves: true,
disabled: false,
proxy: false,
},
setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url);
Expand All @@ -81,18 +90,40 @@ export default defineNuxtModule<ModuleOptions>({
capturePageLeaves: options.capturePageLeaves,
clientOptions: options.clientOptions,
disabled: options.disabled,
proxy: options.proxy,
},
);

const config = nuxt.options.runtimeConfig.public.posthog;

// Make sure url and key are set
const enabled = !nuxt.options.runtimeConfig.public.posthog.disabled;
if (enabled && !nuxt.options.runtimeConfig.public.posthog.publicKey) {
const enabled = !config.disabled;
if (enabled && !config.publicKey) {
console.warn('Missing PostHog API public key, set it either in `nuxt.config.ts` or via env variable');
}
if (enabled && !nuxt.options.runtimeConfig.public.posthog.host) {
if (enabled && !config.host) {
console.warn('Missing PostHog API host, set it either in `nuxt.config.ts` or via env variable');
}

// Setup proxy
if (enabled && config.proxy && config.host) {
const url = new URL(config.host);
const region = url.hostname.split('.')[0];

if (!['eu', 'us'].includes(region)) {
throw new Error(`Invalid PostHog API host when setting proxy, expected 'us' or 'eu', got '${region}'`);
}

nuxt.options.routeRules = nuxt.options.routeRules || {};

nuxt.options.routeRules['/ingest/ph/static/**'] = {
proxy: `https://${region}-assets.i.posthog.com/static/**`,
};
nuxt.options.routeRules['/ingest/ph/**'] = {
proxy: `https://${region}.i.posthog.com/**`,
};
}

nuxt.hook('imports:dirs', (dirs) => {
dirs.push(resolve('./runtime/composables'));
});
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/plugins/posthog.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export default defineNuxtPlugin({
},
});

if (config.proxy && config.host) {
const url = new URL(config.host);
const region = url.hostname.split('.')[0];

clientOptions.ui_host = `https://${region}.posthog.com`;
clientOptions.api_host = `${window.location.origin}/ingest/ph`;
}

const posthogClient = posthog.init(config.publicKey, clientOptions);

const identity = useCookie('ph-identify');
Expand Down