@@ -106,28 +106,32 @@ const sentryTunnelMiddleware = createMiddleware({ type: "request" }).server(
106106) ;
107107
108108// ---------------------------------------------------------------------------
109- // PostHog reverse proxy — the browser SDK targets `/ingest/*` (configured in
110- // routes/__root.tsx) and we forward to PostHog's ingest + asset hosts. Keeps
109+ // PostHog reverse proxy — the browser SDK targets a build-randomized
110+ // first-party path and we forward to PostHog's ingest + asset hosts. Keeps
111111// events flowing past adblockers that match *.posthog.com. See
112112// https://posthog.com/docs/advanced/proxy/cloudflare
113113// ---------------------------------------------------------------------------
114114
115115const POSTHOG_INGEST_HOST = "us.i.posthog.com" ;
116116const POSTHOG_ASSETS_HOST = "us-assets.i.posthog.com" ;
117+ const POSTHOG_PROXY_PATH = `/api/${ ( import . meta. env . VITE_PUBLIC_ANALYTICS_PATH ?? "a" ) . replace (
118+ / ^ \/ + | \/ + $ / g,
119+ "" ,
120+ ) } `;
117121
118122const posthogProxyMiddleware = createMiddleware ( { type : "request" } ) . server (
119123 ( { pathname, request, next } ) => {
120- if ( pathname !== "/ingest" && ! pathname . startsWith ( "/ingest/" ) ) {
124+ if ( pathname !== POSTHOG_PROXY_PATH && ! pathname . startsWith ( ` ${ POSTHOG_PROXY_PATH } /` ) ) {
121125 return next ( ) ;
122126 }
123127
124128 const url = new URL ( request . url ) ;
125- url . hostname = pathname . startsWith ( "/ingest/ static/" )
129+ url . hostname = pathname . startsWith ( ` ${ POSTHOG_PROXY_PATH } / static/` )
126130 ? POSTHOG_ASSETS_HOST
127131 : POSTHOG_INGEST_HOST ;
128132 url . protocol = "https:" ;
129133 url . port = "" ;
130- url . pathname = pathname . replace ( / ^ \/ i n g e s t / , "" ) || "/" ;
134+ url . pathname = pathname . slice ( POSTHOG_PROXY_PATH . length ) || "/" ;
131135
132136 const upstream = new Request ( url , request ) ;
133137 upstream . headers . delete ( "cookie" ) ;
0 commit comments