All files / src/lib sentry-client.ts

90% Statements 9/10
90.9% Branches 10/11
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                            7x   2x   1x   4x                         6x 2x               1x       1x  
// Browser-side Sentry init for the marketplace. Loaded once via a module
// `<script>` in the shared layout head, so it runs on every page.
//
// DSN + environment are build-time values inlined through the astro.config
// `define` block (PUBLIC_SENTRY_DSN / PUBLIC_ENVIRONMENT). When the DSN is
// empty or we're running locally this is a no-op, so the bundle ships but
// never initializes — matching how the API/portal skip Sentry in local dev.
//
// Errors + browser tracing only (no Session Replay): the marketplace is
// LCP-sensitive and Replay roughly doubles the client SDK weight. Add
// `Sentry.replayIntegration()` here if replay is later prioritized.
import * as Sentry from "@sentry/browser";
 
export function tracesSampleRate(environment: string): number {
	switch (environment) {
		case "production":
			return 0.1;
		case "preview":
			return 0.5;
		default:
			return 1.0;
	}
}
 
/**
 * Build the Sentry browser init options, or `null` when Sentry should stay
 * disabled (no DSN, or running locally). Pure + exported so the gating logic
 * is unit-testable without invoking the SDK.
 */
export function sentryClientOptions(
	dsn: string | undefined,
	environment: string,
): Sentry.BrowserOptions | null {
	if (!dsn || environment === "local") return null;
	return {
		dsn,
		environment,
		tracesSampleRate: tracesSampleRate(environment),
		integrations: [Sentry.browserTracingIntegration()],
	};
}
 
const options = sentryClientOptions(
	import.meta.env.PUBLIC_SENTRY_DSN as string | undefined,
	(import.meta.env.PUBLIC_ENVIRONMENT as string | undefined) || "local",
);
Iif (options) Sentry.init(options);