From 1256a15266ec4d676791f2c164ec4c9477edb996 Mon Sep 17 00:00:00 2001 From: Waleed Date: Wed, 28 Jan 2026 23:49:57 -0800 Subject: [PATCH] fix(posthog): move session recording proxy to middleware for large payload support (#3065) Next.js rewrites can strip request bodies for large payloads (1MB+), causing 400 errors from CloudFront. PostHog session recordings require up to 64MB per message. Moving the proxy to middleware ensures proper body passthrough. Co-authored-by: Claude Opus 4.5 --- apps/sim/next.config.ts | 12 ------------ apps/sim/proxy.ts | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 12 deletions(-) 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