Skip to content
Closed
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
11 changes: 1 addition & 10 deletions packages/web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
};
},
Expand Down
6 changes: 3 additions & 3 deletions packages/web/src/app/instrumentation.client.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
export default posthog;
3 changes: 2 additions & 1 deletion packages/web/src/components/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/web/src/lib/posthog-config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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.
// Falls back to the public project key that works in all environments.
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;
25 changes: 25 additions & 0 deletions packages/web/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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();
Expand Down
Loading