-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathsentry.client.config.ts
More file actions
92 lines (82 loc) · 2.72 KB
/
sentry.client.config.ts
File metadata and controls
92 lines (82 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Sentry Client Configuration
* Sentry configuration for client-side error tracking
* Only initializes when SENTRY_DSN is configured and @sentry/nextjs is installed
*
* NOTE: Install @sentry/nextjs package before using: npm install @sentry/nextjs
*/
/**
* Initialize Sentry on the client side
* Only runs in production and when DSN is configured
*/
export default function initSentry(): void {
const dsn = process.env.NEXT_PUBLIC_SENTRY_DSN || process.env.SENTRY_DSN;
// Only initialize in production with valid DSN
if (process.env.NODE_ENV !== "production" || !dsn) {
return;
}
// Dynamic import to handle missing @sentry/nextjs package gracefully
try {
const Sentry = require("@sentry/nextjs");
if (!Sentry || !Sentry.init) {
return;
}
Sentry.init({
dsn,
environment: process.env.NODE_ENV || "production",
tracesSampleRate: 0.1, // Capture 10% of transactions for performance monitoring
replaysSessionSampleRate: 0.1, // Capture 10% of sessions for replay
replaysOnErrorSampleRate: 1.0, // Capture 100% of sessions with errors for replay
// Filter out sensitive data
beforeSend(event: import("@sentry/nextjs").Event, _hint: import("@sentry/nextjs").EventHint) {
// Filter out sensitive fields
if (event.request) {
delete event.request.headers?.["authorization"];
delete event.request.headers?.["cookie"];
delete event.request.cookies;
}
// Filter out sensitive context data
if (event.contexts?.request?.headers) {
const h = event.contexts.request.headers as Record<string, unknown>;
delete h.authorization;
delete h.cookie;
}
return event;
},
// Integrate with React
integrations: [
Sentry.replayIntegration({
maskAllText: true, // Mask all text for privacy
blockAllMedia: true, // Block all media for privacy
}),
Sentry.browserTracingIntegration(),
],
// Ignore specific errors
ignoreErrors: [
// Browser extensions
"top.GLOBALS",
"originalCreateNotification",
"canvas.contentDocument",
"MyApp_RemoveAllHighlights",
"atomicFindClose",
"fb_xd_fragment",
"bmi_SafeAddOnload",
"EBCallBackMessageReceived",
"conduitPage",
// Network errors that are expected
"NetworkError",
"Failed to fetch",
"Network request failed",
// ResizeObserver errors (common and non-critical)
"ResizeObserver loop limit exceeded",
"ResizeObserver loop completed with undelivered notifications",
],
});
} catch {
// @sentry/nextjs not installed, silently fail
}
}
// Initialize Sentry on module load
if (typeof window !== "undefined") {
initSentry();
}