Skip to content

Commit 2489606

Browse files
committed
adding new version
1 parent 6d37151 commit 2489606

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

middleware.ts

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,64 +23,69 @@ function getClientIP(request: NextRequest): string {
2323

2424
export async function middleware(request: NextRequest) {
2525
const startTime = Date.now();
26+
const pathname = request.nextUrl.pathname;
27+
28+
// Only collect analytics for MCP endpoints
29+
const shouldCollectAnalytics = pathname.startsWith('/mcp/');
2630

2731
// Let the request proceed
2832
const response = NextResponse.next();
2933

30-
// For Edge Runtime, we'll send analytics data to our API endpoint
31-
// This avoids Prisma issues in middleware
32-
Promise.resolve().then(async () => {
33-
try {
34-
const endTime = Date.now();
35-
const responseTime = endTime - startTime;
36-
37-
const ip = getClientIP(request);
38-
const userAgent = request.headers.get('user-agent') || '';
39-
40-
// Extract client/user info if available from auth header
41-
let clientId: string | undefined;
42-
let userId: string | undefined;
43-
44-
// Send analytics data to our API endpoint (Edge Runtime compatible)
45-
const analyticsData = {
46-
timestamp: new Date(startTime).toISOString(),
47-
endpoint: request.nextUrl.pathname,
48-
method: request.method,
49-
statusCode: response.status,
50-
responseTime,
51-
clientId,
52-
userId,
53-
ipAddress: ip,
54-
userAgent
55-
};
34+
// Selective analytics collection for MCP endpoints only
35+
if (shouldCollectAnalytics) {
36+
Promise.resolve().then(async () => {
37+
try {
38+
const endTime = Date.now();
39+
const responseTime = endTime - startTime;
40+
41+
const ip = getClientIP(request);
42+
const userAgent = request.headers.get('user-agent') || '';
43+
44+
// Extract client/user info if available from auth header
45+
let clientId: string | undefined;
46+
let userId: string | undefined;
47+
48+
// Send analytics data to our API endpoint (Edge Runtime compatible)
49+
const analyticsData = {
50+
timestamp: new Date(startTime).toISOString(),
51+
endpoint: pathname,
52+
method: request.method,
53+
statusCode: response.status,
54+
responseTime,
55+
clientId,
56+
userId,
57+
ipAddress: ip,
58+
userAgent
59+
};
5660

57-
// Make a fetch call to our analytics API endpoint
58-
// This runs in Edge Runtime and sends data to Node.js runtime
59-
const host = request.headers.get('host');
60-
const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
61-
const baseUrl = `${protocol}://${host}`;
62-
63-
await fetch(`${baseUrl}/api/analytics/collect`, {
64-
method: 'POST',
65-
headers: {
66-
'Content-Type': 'application/json',
67-
},
68-
body: JSON.stringify(analyticsData)
69-
}).catch(() => {
70-
// Silent fail - analytics shouldn't break the main request
71-
});
72-
73-
} catch (error) {
74-
console.warn('Analytics collection failed:', error);
75-
}
76-
});
61+
// Make a fetch call to our analytics API endpoint
62+
// This runs in Edge Runtime and sends data to Node.js runtime
63+
const host = request.headers.get('host');
64+
const protocol = process.env.NODE_ENV === 'production' ? 'https' : 'http';
65+
const baseUrl = `${protocol}://${host}`;
66+
67+
await fetch(`${baseUrl}/api/analytics/collect`, {
68+
method: 'POST',
69+
headers: {
70+
'Content-Type': 'application/json',
71+
},
72+
body: JSON.stringify(analyticsData)
73+
}).catch(() => {
74+
// Silent fail - analytics shouldn't break the main request
75+
});
76+
77+
} catch (error) {
78+
console.warn('Analytics collection failed:', error);
79+
}
80+
});
81+
}
7782

7883
return response;
7984
}
8085

8186
export const config = {
8287
matcher: [
83-
// Match all request paths except static files and images
84-
'/((?!_next/static|_next/image|favicon.ico|.*\\.png$|.*\\.jpg$|.*\\.jpeg$|.*\\.gif$|.*\\.svg$).*)',
88+
// Only match MCP endpoints for analytics collection
89+
'/mcp/:path*',
8590
],
8691
};

0 commit comments

Comments
 (0)