@@ -3,52 +3,6 @@ import { type NextRequest, NextResponse } from "next/server";
33const POSTHOG_INGEST_HOST = "us.i.posthog.com" ;
44const POSTHOG_ASSETS_HOST = "us-assets.i.posthog.com" ;
55
6- const HEADERS_TO_STRIP = [
7- // CloudFront headers
8- "cloudfront-forwarded-proto" ,
9- "cloudfront-is-android-viewer" ,
10- "cloudfront-is-desktop-viewer" ,
11- "cloudfront-is-ios-viewer" ,
12- "cloudfront-is-mobile-viewer" ,
13- "cloudfront-is-smarttv-viewer" ,
14- "cloudfront-is-tablet-viewer" ,
15- "cloudfront-viewer-address" ,
16- "cloudfront-viewer-asn" ,
17- "cloudfront-viewer-city" ,
18- "cloudfront-viewer-country" ,
19- "cloudfront-viewer-country-name" ,
20- "cloudfront-viewer-country-region" ,
21- "cloudfront-viewer-country-region-name" ,
22- "cloudfront-viewer-http-version" ,
23- "cloudfront-viewer-latitude" ,
24- "cloudfront-viewer-longitude" ,
25- "cloudfront-viewer-metro-code" ,
26- "cloudfront-viewer-postal-code" ,
27- "cloudfront-viewer-time-zone" ,
28- "cloudfront-viewer-tls" ,
29- // Vercel headers
30- "x-vercel-id" ,
31- "x-vercel-ip-as-number" ,
32- "x-vercel-ip-city" ,
33- "x-vercel-ip-continent" ,
34- "x-vercel-ip-country" ,
35- "x-vercel-ip-country-region" ,
36- "x-vercel-ip-latitude" ,
37- "x-vercel-ip-longitude" ,
38- "x-vercel-ip-postal-code" ,
39- "x-vercel-ip-timezone" ,
40- "x-vercel-ja3-digest" ,
41- "x-vercel-ja4-digest" ,
42- "x-vercel-proxied-for"
43- ] ;
44-
45- function stripHeaders ( headers : Headers ) : Headers {
46- for ( const header of HEADERS_TO_STRIP ) {
47- headers . delete ( header ) ;
48- }
49- return headers ;
50- }
51-
526/**
537 * adapted from https://posthog.com/docs/advanced/proxy/nextjs-middleware
548 *
@@ -70,8 +24,10 @@ function filterAsciiCookies(cookieHeader: string | null): string | undefined {
7024 ) ;
7125}
7226
73- export async function rewritePosthog ( request : NextRequest ) : Promise < NextResponse > {
27+ export function rewritePosthog ( request : NextRequest ) : NextResponse {
7428 try {
29+ const url = request . nextUrl . clone ( ) ;
30+
7531 // More robust pathname extraction
7632 const pathnameParts = request . nextUrl . pathname . split ( "/api/fern-docs/analytics/posthog" ) ;
7733 const intendedPathname = pathnameParts [ 1 ] || "/" ;
@@ -80,9 +36,6 @@ export async function rewritePosthog(request: NextRequest): Promise<NextResponse
8036
8137 const requestHeaders = new Headers ( request . headers ) ;
8238
83- // Strip CloudFront and Vercel infrastructure headers
84- stripHeaders ( requestHeaders ) ;
85-
8639 // Remove any non-ASCII cookies from the request headers
8740 if ( requestHeaders . has ( "cookie" ) ) {
8841 const filteredCookie = filterAsciiCookies ( requestHeaders . get ( "cookie" ) ) ;
@@ -95,29 +48,23 @@ export async function rewritePosthog(request: NextRequest): Promise<NextResponse
9548
9649 requestHeaders . set ( "host" , hostname ) ;
9750
98- // Construct the PostHog URL
99- const posthogUrl = `https://${ hostname } ${ intendedPathname } ` ;
100-
101- // Fetch from PostHog directly instead of rewriting
102- const posthogResponse = await fetch ( posthogUrl , {
103- method : request . method ,
104- headers : requestHeaders ,
105- body : request . method !== "GET" && request . method !== "HEAD" ? await request . text ( ) : undefined
106- } ) ;
51+ // Ensure URL is properly constructed
52+ url . pathname = intendedPathname ;
53+ url . protocol = "https" ;
54+ url . hostname = hostname ;
55+ url . port = "443" ;
56+ url . search = "" ; // Clear search params to avoid issues
10757
108- // Create response with PostHog's body and status
109- const responseHeaders = new Headers ( posthogResponse . headers ) ;
110-
111- // Strip infrastructure headers from the response
112- stripHeaders ( responseHeaders ) ;
58+ // Validate URL before rewrite
59+ if ( ! url . hostname || ! url . protocol ) {
60+ throw new Error ( "Invalid URL constructed" ) ;
61+ }
11362
114- return new NextResponse ( posthogResponse . body , {
115- status : posthogResponse . status ,
116- statusText : posthogResponse . statusText ,
117- headers : responseHeaders
63+ return NextResponse . rewrite ( url , {
64+ headers : requestHeaders
11865 } ) ;
11966 } catch ( error ) {
120- console . error ( "Error proxying to PostHog:" , error ) ;
67+ console . error ( "Error rewriting PostHog URL :" , error ) ;
12168 // Always return 200 even if there's an error
12269 return new NextResponse ( JSON . stringify ( { success : true } ) , {
12370 status : 200 ,
0 commit comments