diff --git a/packages/web/next.config.js b/packages/web/next.config.js index b1169372..7a65d422 100644 --- a/packages/web/next.config.js +++ b/packages/web/next.config.js @@ -67,16 +67,7 @@ const nextConfig = { destination: '/ai', }, ], - afterFiles: [ - { - source: '/ingest/static/:path*', - destination: 'https://us-assets.i.posthog.com/static/:path*', - }, - { - source: '/ingest/:path*', - destination: 'https://us.i.posthog.com/:path*', - }, - ], + afterFiles: [], fallback: [], }; }, diff --git a/packages/web/src/app/instrumentation.client.ts b/packages/web/src/app/instrumentation.client.ts index d48b3f6d..d563fb00 100644 --- a/packages/web/src/app/instrumentation.client.ts +++ b/packages/web/src/app/instrumentation.client.ts @@ -1,10 +1,10 @@ import posthog from 'posthog-js' -import { POSTHOG_HOST } from '@/lib/posthog-config' +import { POSTHOG_API_HOST, POSTHOG_HOST } from '@/lib/posthog-config' // Initialize PostHog as early as possible on the client. This file is automatically // executed by Next.js when it exists at the root of the `app` directory. posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY!, { - api_host: '/ingest', + api_host: POSTHOG_API_HOST, ui_host: POSTHOG_HOST, capture_pageview: false, capture_pageleave: true, @@ -63,4 +63,4 @@ if (process.env.NODE_ENV === 'production') { } // Re-export for convenience so it can be imported from this module elsewhere. -export default posthog; \ No newline at end of file +export default posthog; diff --git a/packages/web/src/components/providers.tsx b/packages/web/src/components/providers.tsx index 57ab834b..705f5953 100644 --- a/packages/web/src/components/providers.tsx +++ b/packages/web/src/components/providers.tsx @@ -10,13 +10,14 @@ import { http } from 'wagmi'; import { SmartWalletsProvider } from '@privy-io/react-auth/smart-wallets'; import SuspendedPostHogPageView from './posthog-pageview'; import { PostHogUserIdentification } from './posthog-user-identification'; +import { POSTHOG_API_HOST } from '@/lib/posthog-config'; import posthog from 'posthog-js'; import { PostHogProvider } from 'posthog-js/react'; if (typeof window !== 'undefined') { posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, { - api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST, + api_host: POSTHOG_API_HOST, capture_pageview: false, // Disable automatic pageview capture, as we capture manually }); } diff --git a/packages/web/src/lib/posthog-config.ts b/packages/web/src/lib/posthog-config.ts index 1045f0bd..e64ec3eb 100644 --- a/packages/web/src/lib/posthog-config.ts +++ b/packages/web/src/lib/posthog-config.ts @@ -1,4 +1,5 @@ export const POSTHOG_HOST = 'https://us.i.posthog.com'; +export const POSTHOG_PROXY_PATH = '/ow'; export function getPostHogKey(): string { // Prefer the env-provided key so it can vary between environments. @@ -6,4 +7,4 @@ export function getPostHogKey(): string { return process.env.NEXT_PUBLIC_POSTHOG_KEY || 'phc_HxAOuIz9mTAqksVrWCEH5eJmRCZf4Ehd4TINbivkvoI'; } -export const POSTHOG_API_HOST = process.env.NEXT_PUBLIC_POSTHOG_HOST || POSTHOG_HOST; +export const POSTHOG_API_HOST = process.env.NEXT_PUBLIC_POSTHOG_HOST || POSTHOG_PROXY_PATH; diff --git a/packages/web/src/proxy.ts b/packages/web/src/proxy.ts index dd3fa23e..57630cd1 100644 --- a/packages/web/src/proxy.ts +++ b/packages/web/src/proxy.ts @@ -1,9 +1,14 @@ import { NextResponse } from 'next/server'; import type { NextRequest } from 'next/server'; +const POSTHOG_PROXY_PATH = '/ow'; +const POSTHOG_API_HOST = 'us.i.posthog.com'; +const POSTHOG_ASSETS_HOST = 'us-assets.i.posthog.com'; + // Force Node.js runtime to support Privy server-auth crypto module export const config = { matcher: [ + '/ow/:path*', /* * Match all request paths except for the ones starting with: * - api (API routes) @@ -20,6 +25,26 @@ export const config = { export async function proxy(request: NextRequest) { const { pathname } = request.nextUrl; + if (pathname.startsWith(POSTHOG_PROXY_PATH)) { + const url = request.nextUrl.clone(); + const hostname = pathname.startsWith(`${POSTHOG_PROXY_PATH}/static/`) + ? POSTHOG_ASSETS_HOST + : POSTHOG_API_HOST; + const requestHeaders = new Headers(request.headers); + + requestHeaders.set('host', hostname); + requestHeaders.delete('cookie'); + + url.protocol = 'https'; + url.hostname = hostname; + url.port = '443'; + url.pathname = pathname.replace(/^\/ow/, '') || '/'; + + return NextResponse.rewrite(url, { + headers: requestHeaders, + }); + } + // Skip auth checks for demo route if (pathname.startsWith('/dashboard/demo')) { return NextResponse.next();