Skip to content

Commit 1245328

Browse files
authored
fix(analytics): filter cookies in Amplitude proxy to prevent request rejections
Create custom API route handler for /api/collect that removes all cookies before proxying to Amplitude's API. This prevents Amplitude from rejecting requests due to excessive or irrelevant cookies while maintaining analytics functionality. - Add /api/collect API route that proxies to Amplitude with cookie filtering - Rewrite URL path from /api/collect to /2/httpapi to match Amplitude endpoint - Initialize proxy server once and reuse for better performance - Remove Next.js rewrite rule as API route takes precedence
1 parent 5a1ed1f commit 1245328

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

apps/deploy-web/next.config.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,6 @@ const withPWA = require("next-pwa")({
2323
const { withSentryConfig } = require("@sentry/nextjs");
2424
const path = require("path");
2525

26-
let browserEnv;
27-
try {
28-
const { browserEnvSchema } = require("./env-config.schema");
29-
30-
browserEnv = browserEnvSchema.parse(process.env);
31-
} catch (error) {
32-
if (error.message.includes("Cannot find module")) {
33-
console.warn("No env-config.schema.js found, skipping env validation");
34-
}
35-
}
36-
3726
const transpilePackages = ["geist", "@akashnetwork/ui", "@auth0/nextjs-auth0"];
3827

3928
if (process.env.NODE_ENV === "test") {
@@ -170,16 +159,7 @@ const nextConfig = {
170159
permanent: false
171160
}
172161
];
173-
},
174-
rewrites: async () =>
175-
browserEnv.NEXT_PUBLIC_AMPLITUDE_PROXY_URL
176-
? [
177-
{
178-
source: browserEnv.NEXT_PUBLIC_AMPLITUDE_PROXY_URL,
179-
destination: "https://api2.amplitude.com/2/httpapi"
180-
}
181-
]
182-
: []
162+
}
183163
};
184164

185165
const REPOSITORY_URL = new URL(repository.url);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { defineApiHandler } from "@src/lib/nextjs/defineApiHandler/defineApiHandler";
2+
import type { AppServices } from "@src/services/app-di-container/server-di-container.service";
3+
4+
let amplitudeProxy: ReturnType<AppServices["httpProxy"]["createProxyServer"]> | null = null;
5+
6+
function getAmplitudeProxy(services: AppServices) {
7+
if (!amplitudeProxy) {
8+
amplitudeProxy = services.httpProxy.createProxyServer({
9+
changeOrigin: true,
10+
target: "https://api2.amplitude.com",
11+
secure: true,
12+
autoRewrite: false
13+
});
14+
}
15+
16+
return amplitudeProxy;
17+
}
18+
19+
export default defineApiHandler({
20+
route: "/api/collect",
21+
async handler({ req, res, services }) {
22+
delete req.headers.cookie;
23+
24+
req.url = "/2/httpapi";
25+
26+
const proxy = getAmplitudeProxy(services);
27+
28+
return new Promise<void>((resolve, reject) => {
29+
const onProxyRes = (_proxyRes: unknown, _req: unknown, proxyRes: unknown) => {
30+
if (proxyRes === res) {
31+
proxy.off("error", onError);
32+
resolve();
33+
}
34+
};
35+
const onError = (error: Error, _req: unknown, errorRes: unknown) => {
36+
if (errorRes === res) {
37+
proxy.off("proxyRes", onProxyRes);
38+
services.logger.error({ error, event: "AMPLITUDE_PROXY_ERROR" });
39+
if (!res.headersSent) {
40+
res.status(502).json({ error: "Proxy error" });
41+
}
42+
reject(error);
43+
}
44+
};
45+
proxy.on("proxyRes", onProxyRes);
46+
proxy.on("error", onError);
47+
proxy.web(req, res);
48+
});
49+
}
50+
});
51+
52+
export const config = {
53+
api: {
54+
externalResolver: true,
55+
bodyParser: false
56+
}
57+
};

0 commit comments

Comments
 (0)