Skip to content

An AI-powered search engine with a generative UI

Proxy PostHog through /relay (#872)

Yoshiki Miurab8735abaa60c
diff --git a/lib/analytics/posthog-client.ts b/lib/analytics/posthog-client.ts
index e1e864dd..f028d3fa 100644
--- a/lib/analytics/posthog-client.ts
+++ b/lib/analytics/posthog-client.ts
@@ -14,8 +14,14 @@ export function initPostHog(): void {
const key = clientKey()
if (!key) return
+ // The /relay reverse proxy (next.config rewrites) only targets US cloud.
+ // EU / self-hosted deployments keep talking to their configured host directly.
+ const host = process.env.NEXT_PUBLIC_POSTHOG_HOST
+ const useRelay = !host || host.includes('us.i.posthog.com')
+
posthog.init(key, {
- api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
+ api_host: useRelay ? '/relay' : host,
+ ...(useRelay ? { ui_host: 'https://us.posthog.com' } : {}),
autocapture: false,
capture_pageview: false,
disable_session_recording: true,
diff --git a/next.config.mjs b/next.config.mjs
index 325d5e2a..e9e12f04 100644
--- a/next.config.mjs
+++ b/next.config.mjs
@@ -1,5 +1,23 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
+ // Reverse proxy for PostHog to reduce tracking-blocker interception.
+ skipTrailingSlashRedirect: true,
+ async rewrites() {
+ return [
+ {
+ source: '/relay/static/:path*',
+ destination: 'https://us-assets.i.posthog.com/static/:path*'
+ },
+ {
+ source: '/relay/array/:path*',
+ destination: 'https://us-assets.i.posthog.com/array/:path*'
+ },
+ {
+ source: '/relay/:path*',
+ destination: 'https://us.i.posthog.com/:path*'
+ }
+ ]
+ },
images: {
remotePatterns: [
{