| @@ -106,28 +106,32 @@ const sentryTunnelMiddleware = createMiddleware({ type: "request" }).server( · 105 unmodified lines |
| 106 | ); | 106 | ); |
| 107 | | 107 | |
| 108 | // --------------------------------------------------------------------------- | 108 | // --------------------------------------------------------------------------- |
| 109 | // PostHog reverse proxy — the browser SDK targets `/ingest/*` (configured in | 109 | // PostHog reverse proxy — the browser SDK targets a build-randomized |
| 110 | // routes/__root.tsx) and we forward to PostHog's ingest + asset hosts. Keeps | 110 | // first-party path and we forward to PostHog's ingest + asset hosts. Keeps |
| 111 | // events flowing past adblockers that match *.posthog.com. See | 111 | // events flowing past adblockers that match *.posthog.com. See |
| 112 | // https://posthog.com/docs/advanced/proxy/cloudflare | 112 | // https://posthog.com/docs/advanced/proxy/cloudflare |
| 113 | // --------------------------------------------------------------------------- | 113 | // --------------------------------------------------------------------------- |
| 114 | | 114 | |
| 115 | const POSTHOG_INGEST_HOST = "us.i.posthog.com"; | 115 | const POSTHOG_INGEST_HOST = "us.i.posthog.com"; |
| 116 | const POSTHOG_ASSETS_HOST = "us-assets.i.posthog.com"; | 116 | const 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 | )}`; |
| 117 | | 121 | |
| 118 | const posthogProxyMiddleware = createMiddleware({ type: "request" }).server( | 122 | const posthogProxyMiddleware = createMiddleware({ type: "request" }).server( |
| 119 | ({ pathname, request, next }) => { | 123 | ({ pathname, request, next }) => { |
| 120 | if (pathname !== "/ingest" && !pathname.startsWith("/ingest/")) { | 124 | if (pathname !== POSTHOG_PROXY_PATH && !pathname.startsWith(`${POSTHOG_PROXY_PATH}/`)) { |
| 121 | return next(); | 125 | return next(); |
| 122 | } | 126 | } |
| 123 | | 127 | |
| 124 | const url = new URL(request.url); | 128 | const url = new URL(request.url); |
| 125 | url.hostname = pathname.startsWith("/ingest/static/") | 129 | url.hostname = pathname.startsWith(`${POSTHOG_PROXY_PATH}/static/`) |
| 126 | ? POSTHOG_ASSETS_HOST | 130 | ? POSTHOG_ASSETS_HOST |
| 127 | : POSTHOG_INGEST_HOST; | 131 | : POSTHOG_INGEST_HOST; |
| 128 | url.protocol = "https:"; | 132 | url.protocol = "https:"; |
| 129 | url.port = ""; | 133 | url.port = ""; |
| 130 | url.pathname = pathname.replace(/^\/ingest/, "") || "/"; | 134 | url.pathname = pathname.slice(POSTHOG_PROXY_PATH.length) || "/"; |
| 131 | | 135 | |
| 132 | const upstream = new Request(url, request); | 136 | const upstream = new Request(url, request); |
| 133 | upstream.headers.delete("cookie"); | 137 | upstream.headers.delete("cookie"); |