Skip to content

Commit 6f87ba2

Browse files
authored
Proxy Den PostHog requests through /ow (anomalyco#893)
1 parent 2b7dc4a commit 6f87ba2

4 files changed

Lines changed: 45 additions & 4 deletions

File tree

‎packages/web/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ Frontend for `app.openwork.software`.
3535
- this host must serve `/api/auth/*`; the included proxy route does that
3636
- `NEXT_PUBLIC_POSTHOG_KEY` (client): PostHog project key used for Den analytics.
3737
- optional override; defaults to the same project key used by `packages/landing`
38-
- `NEXT_PUBLIC_POSTHOG_HOST` (client): PostHog host URL.
39-
- default: `https://us.i.posthog.com`
38+
- `NEXT_PUBLIC_POSTHOG_HOST` (client): PostHog ingest host or same-origin proxy path.
39+
- default: `/ow`
40+
- set it to `https://us.i.posthog.com` to bypass the local proxy
4041
- `LOOPS_API_KEY` (server-only): Loops API key for signup contact capture.
4142

4243
## Deploy on Vercel

‎packages/web/app/layout.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ export const metadata: Metadata = {
2323
};
2424

2525
const defaultPosthogKey = "phc_4YnPTlDVYPjgwKvLuNxhbHjV5kadgvd7XLzVHWnCXAI";
26+
const defaultPosthogProxyPath = "/ow";
2627
const posthogKey =
2728
process.env.NEXT_PUBLIC_POSTHOG_KEY?.trim() ||
2829
process.env.NEXT_PUBLIC_POSTHOG_API_KEY?.trim() ||
2930
defaultPosthogKey;
30-
const posthogHost = (process.env.NEXT_PUBLIC_POSTHOG_HOST ?? "https://us.i.posthog.com").trim();
31+
const posthogHost = (process.env.NEXT_PUBLIC_POSTHOG_HOST ?? defaultPosthogProxyPath).trim();
3132

3233
const posthogBootstrap = posthogKey
3334
? `!function(t,e){var o,n,p,r;e.__SV||(window.posthog&&window.posthog.__loaded)||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="init capture identify alias reset register unregister setPersonProperties".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])})}(document,window.posthog||[]);

‎packages/web/middleware.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { NextResponse } from "next/server";
2+
import type { NextRequest } from "next/server";
3+
4+
const POSTHOG_PROXY_PATH = "/ow";
5+
const POSTHOG_API_HOST = "us.i.posthog.com";
6+
const POSTHOG_ASSETS_HOST = "us-assets.i.posthog.com";
7+
8+
export function middleware(request: NextRequest) {
9+
const url = request.nextUrl.clone();
10+
const { pathname } = url;
11+
12+
if (!pathname.startsWith(POSTHOG_PROXY_PATH)) {
13+
return NextResponse.next();
14+
}
15+
16+
const hostname = pathname.startsWith(`${POSTHOG_PROXY_PATH}/static/`)
17+
? POSTHOG_ASSETS_HOST
18+
: POSTHOG_API_HOST;
19+
const requestHeaders = new Headers(request.headers);
20+
21+
requestHeaders.set("host", hostname);
22+
requestHeaders.delete("cookie");
23+
24+
url.protocol = "https";
25+
url.hostname = hostname;
26+
url.port = "443";
27+
url.pathname = pathname.replace(/^\/ow/, "") || "/";
28+
29+
return NextResponse.rewrite(url, {
30+
request: {
31+
headers: requestHeaders
32+
}
33+
});
34+
}
35+
36+
export const config = {
37+
matcher: "/ow/:path*"
38+
};

‎packages/web/next.config.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** @type {import('next').NextConfig} */
22
const nextConfig = {
3-
reactStrictMode: true
3+
reactStrictMode: true,
4+
skipTrailingSlashRedirect: true
45
};
56

67
module.exports = nextConfig;

0 commit comments

Comments
 (0)