diff --git a/apps/sim/next.config.ts b/apps/sim/next.config.ts index 45f6ffecd..bbeb57c94 100644 --- a/apps/sim/next.config.ts +++ b/apps/sim/next.config.ts @@ -325,18 +325,6 @@ const nextConfig: NextConfig = { return redirects }, - async rewrites() { - return [ - { - source: '/ingest/static/:path*', - destination: 'https://us-assets.i.posthog.com/static/:path*', - }, - { - source: '/ingest/:path*', - destination: 'https://us.i.posthog.com/:path*', - }, - ] - }, } export default nextConfig diff --git a/apps/sim/proxy.ts b/apps/sim/proxy.ts index 0d9f5aaa8..773700a75 100644 --- a/apps/sim/proxy.ts +++ b/apps/sim/proxy.ts @@ -134,6 +134,24 @@ function handleSecurityFiltering(request: NextRequest): NextResponse | null { export async function proxy(request: NextRequest) { const url = request.nextUrl + if (url.pathname.startsWith('/ingest/')) { + const hostname = url.pathname.startsWith('/ingest/static/') + ? 'us-assets.i.posthog.com' + : 'us.i.posthog.com' + + const targetPath = url.pathname.replace(/^\/ingest/, '') + const targetUrl = `https://${hostname}${targetPath}${url.search}` + + return NextResponse.rewrite(new URL(targetUrl), { + request: { + headers: new Headers({ + ...Object.fromEntries(request.headers), + host: hostname, + }), + }, + }) + } + const sessionCookie = getSessionCookie(request) const hasActiveSession = isAuthDisabled || !!sessionCookie @@ -195,6 +213,7 @@ export async function proxy(request: NextRequest) { export const config = { matcher: [ + '/ingest/:path*', // PostHog proxy for session recording '/', // Root path for self-hosted redirect logic '/terms', // Whitelabel terms redirect '/privacy', // Whitelabel privacy redirect