fix(ui): skip Sentry initialization when DSN is not configured (#9368)

This commit is contained in:
Alan Buscaglia
2025-12-01 18:05:45 +01:00
committed by GitHub
parent 56ea498cca
commit dda0a2567d
4 changed files with 231 additions and 206 deletions

View File

@@ -11,9 +11,13 @@
import { browserTracingIntegration } from "@sentry/browser";
import * as Sentry from "@sentry/nextjs";
const isDevelopment = process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT === "local";
const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN;
/**
// Only initialize Sentry if DSN is configured
if (SENTRY_DSN) {
const isDevelopment = process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT === "local";
/**
* Initialize Sentry error tracking and performance monitoring
*
* This setup includes:
@@ -21,9 +25,9 @@ const isDevelopment = process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT === "local";
* - Long task detection for UI-blocking operations
* - beforeSend hook to filter noise
*/
Sentry.init({
Sentry.init({
// 📍 DSN - Data Source Name (identifies your Sentry project)
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
dsn: SENTRY_DSN,
// 🌍 Environment - Separate dev errors from production
environment: process.env.NEXT_PUBLIC_SENTRY_ENVIRONMENT || "local",
@@ -104,12 +108,13 @@ Sentry.init({
return event; // Send to Sentry
},
});
});
// 👤 Set user context (identifies who experienced the error)
// In production, this will be updated after authentication
if (isDevelopment) {
// 👤 Set user context (identifies who experienced the error)
// In production, this will be updated after authentication
if (isDevelopment) {
Sentry.setUser({
id: "dev-user",
});
}
}

View File

@@ -16,7 +16,14 @@
import * as Sentry from "@sentry/nextjs";
const SENTRY_DSN = process.env.SENTRY_DSN;
export async function register() {
// Skip Sentry initialization if DSN is not configured
if (!SENTRY_DSN) {
return;
}
// The Sentry SDK automatically loads the appropriate config based on runtime
if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./sentry/sentry.server.config");
@@ -27,4 +34,7 @@ export async function register() {
}
}
export const onRequestError = Sentry.captureRequestError;
// Only capture request errors if Sentry is configured
export const onRequestError = SENTRY_DSN
? Sentry.captureRequestError
: undefined;

View File

@@ -1,8 +1,12 @@
import * as Sentry from "@sentry/nextjs";
const isProduction = process.env.SENTRY_ENVIRONMENT === "pro";
const SENTRY_DSN = process.env.SENTRY_DSN;
/**
// Only initialize Sentry if DSN is configured
if (SENTRY_DSN) {
const isProduction = process.env.SENTRY_ENVIRONMENT === "pro";
/**
* Edge runtime Sentry configuration
*
* Edge runtime has stricter constraints than Node.js:
@@ -11,9 +15,9 @@ const isProduction = process.env.SENTRY_ENVIRONMENT === "pro";
* - Reduced sample rates to minimize overhead
* - No complex integrations
*/
Sentry.init({
Sentry.init({
// 📍 DSN - Data Source Name (identifies your Sentry project)
dsn: process.env.SENTRY_DSN,
dsn: SENTRY_DSN,
// 🌍 Environment configuration
environment: process.env.SENTRY_ENVIRONMENT || "local",
@@ -61,4 +65,5 @@ Sentry.init({
return event;
},
});
});
}

View File

@@ -1,8 +1,12 @@
import * as Sentry from "@sentry/nextjs";
const isProduction = process.env.SENTRY_ENVIRONMENT === "pro";
const SENTRY_DSN = process.env.SENTRY_DSN;
/**
// Only initialize Sentry if DSN is configured
if (SENTRY_DSN) {
const isProduction = process.env.SENTRY_ENVIRONMENT === "pro";
/**
* Server-side Sentry configuration
*
* This setup includes:
@@ -10,9 +14,9 @@ const isProduction = process.env.SENTRY_ENVIRONMENT === "pro";
* - Error tracking for API routes and server actions
* - beforeSend hook to filter noise and add context
*/
Sentry.init({
Sentry.init({
// 📍 DSN - Data Source Name (identifies your Sentry project)
dsn: process.env.SENTRY_DSN,
dsn: SENTRY_DSN,
// 🌍 Environment configuration
environment: process.env.SENTRY_ENVIRONMENT || "local",
@@ -77,4 +81,5 @@ Sentry.init({
return event;
},
});
});
}