Skip to content

Conversation

@waleedlatif1
Copy link
Collaborator

Summary

  • Replaced NextResponse.rewrite() PostHog proxy with a dedicated route handler at /ingest/[[...path]]
  • Uses fetch() with duplex: 'half' for reliable request body streaming (fixes ECONNRESET errors on large payloads like session recordings)
  • Excluded /ingest from proxy matcher to prevent Next.js 16 body buffering interference

Type of Change

  • Bug fix

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel
Copy link

vercel bot commented Feb 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Feb 11, 2026 3:49am

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 11, 2026

Greptile Overview

Greptile Summary

Replaced the PostHog proxy implementation from NextResponse.rewrite() in middleware to a dedicated route handler at /ingest/[[...path]]. The new implementation uses fetch() with duplex: 'half' to properly stream request bodies, which resolves ECONNRESET errors that occurred with large payloads like session recordings. The middleware matcher now excludes /ingest to prevent Next.js 16 from buffering request bodies, which was interfering with the proxy functionality.

Key changes:

  • Created new route handler with proper header forwarding (x-forwarded-for for geolocation, strips cookies/connection headers)
  • Routes /ingest/static/* to us-assets.i.posthog.com, other paths to us.i.posthog.com
  • Includes error handling with structured logging and 502 status on failures
  • Removed duplicate PostHog logic from proxy.ts middleware

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The change is a well-implemented bug fix that moves PostHog proxying to a more appropriate location. The code follows best practices with proper error handling, logging, and header management. The implementation correctly uses duplex: 'half' to enable request body streaming, which is the documented solution for this use case.
  • No files require special attention

Important Files Changed

Filename Overview
apps/sim/app/ingest/[[...path]]/route.ts New route handler for PostHog proxy using fetch() with duplex: 'half' to reliably stream request bodies and avoid ECONNRESET errors. Well-structured with proper error handling and logging.
apps/sim/proxy.ts Removed PostHog proxy logic from middleware (previously using NextResponse.rewrite()). Added ingest to exclusion pattern in matcher to prevent middleware from intercepting the new route handler.

Sequence Diagram

sequenceDiagram
    participant Client
    participant NextMiddleware as Next.js Middleware (proxy.ts)
    participant RouteHandler as Route Handler (/ingest/[[...path]])
    participant PostHogAPI as PostHog API (us.i.posthog.com)
    participant PostHogAssets as PostHog Assets (us-assets.i.posthog.com)

    Note over Client,PostHogAssets: Before: Used NextResponse.rewrite() in middleware
    Note over Client,PostHogAssets: After: Dedicated route handler with fetch()

    Client->>NextMiddleware: POST /ingest/... (large payload)
    Note over NextMiddleware: Excluded from matcher<br/>(prevents body buffering)
    NextMiddleware->>RouteHandler: Pass through
    
    alt Static asset request (/ingest/static/*)
        RouteHandler->>PostHogAssets: fetch() with duplex: 'half'
        PostHogAssets-->>RouteHandler: Asset response
    else API request
        RouteHandler->>PostHogAPI: fetch() with duplex: 'half'<br/>(streams body without buffering)
        PostHogAPI-->>RouteHandler: API response
    end
    
    RouteHandler-->>Client: Streamed response
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@waleedlatif1
Copy link
Collaborator Author

@cursor review

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

@waleedlatif1 waleedlatif1 merged commit c471627 into staging Feb 11, 2026
12 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/posthog branch February 11, 2026 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant