| @@ -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 |
| 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 |
| 111 | 111 | // events flowing past adblockers that match *.posthog.com. See |
| 112 | 112 | // https://posthog.com/docs/advanced/proxy/cloudflare |
| 113 | 113 | // --------------------------------------------------------------------------- |
| 114 | 114 | |
| 115 | 115 | const POSTHOG_INGEST_HOST = "us.i.posthog.com"; |
| 116 | 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 | 122 | const posthogProxyMiddleware = createMiddleware({ type: "request" }).server( |
| 119 | 123 | ({ pathname, request, next }) => { |
| 120 | | − if (pathname !== "/ingest" && !pathname.startsWith("/ingest/")) { |
| 124 | + if (pathname !== POSTHOG_PROXY_PATH && !pathname.startsWith(`${POSTHOG_PROXY_PATH}/`)) { |
| 121 | 125 | return next(); |
| 122 | 126 | } |
| 123 | 127 | |
| 124 | 128 | const url = new URL(request.url); |
| 125 | | − url.hostname = pathname.startsWith("/ingest/static/") |
| 129 | + url.hostname = pathname.startsWith(`${POSTHOG_PROXY_PATH}/static/`) |
| 126 | 130 | ? POSTHOG_ASSETS_HOST |
| 127 | 131 | : POSTHOG_INGEST_HOST; |
| 128 | 132 | url.protocol = "https:"; |
| 129 | 133 | url.port = ""; |
| 130 | | − url.pathname = pathname.replace(/^\/ingest/, "") || "/"; |
| 134 | + url.pathname = pathname.slice(POSTHOG_PROXY_PATH.length) || "/"; |
| 131 | 135 | |
| 132 | 136 | const upstream = new Request(url, request); |
| 133 | 137 | upstream.headers.delete("cookie"); |