Skip to content

Commit 8863da8

Browse files
waleedlatif1claude
andcommitted
fix(posthog): move session recording proxy to middleware for large payload support
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 <noreply@anthropic.com>
1 parent c8e0c4d commit 8863da8

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

apps/sim/next.config.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -325,18 +325,6 @@ const nextConfig: NextConfig = {
325325

326326
return redirects
327327
},
328-
async rewrites() {
329-
return [
330-
{
331-
source: '/ingest/static/:path*',
332-
destination: 'https://us-assets.i.posthog.com/static/:path*',
333-
},
334-
{
335-
source: '/ingest/:path*',
336-
destination: 'https://us.i.posthog.com/:path*',
337-
},
338-
]
339-
},
340328
}
341329

342330
export default nextConfig

apps/sim/proxy.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ function handleSecurityFiltering(request: NextRequest): NextResponse | null {
134134
export async function proxy(request: NextRequest) {
135135
const url = request.nextUrl
136136

137+
if (url.pathname.startsWith('/ingest/')) {
138+
const hostname = url.pathname.startsWith('/ingest/static/')
139+
? 'us-assets.i.posthog.com'
140+
: 'us.i.posthog.com'
141+
142+
const targetPath = url.pathname.replace(/^\/ingest/, '')
143+
const targetUrl = `https://${hostname}${targetPath}${url.search}`
144+
145+
return NextResponse.rewrite(new URL(targetUrl), {
146+
request: {
147+
headers: new Headers({
148+
...Object.fromEntries(request.headers),
149+
host: hostname,
150+
}),
151+
},
152+
})
153+
}
154+
137155
const sessionCookie = getSessionCookie(request)
138156
const hasActiveSession = isAuthDisabled || !!sessionCookie
139157

@@ -195,6 +213,7 @@ export async function proxy(request: NextRequest) {
195213

196214
export const config = {
197215
matcher: [
216+
'/ingest/:path*', // PostHog proxy for session recording
198217
'/', // Root path for self-hosted redirect logic
199218
'/terms', // Whitelabel terms redirect
200219
'/privacy', // Whitelabel privacy redirect

0 commit comments

Comments
 (0)