From 70f327c0dea6a0b3f16419b48b9d24e69244c93f Mon Sep 17 00:00:00 2001 From: Andrei Borza Date: Fri, 30 Aug 2024 23:17:53 +0200 Subject: [PATCH] feat(solidstart): Add `browserTracingIntegration` by default --- packages/solidstart/src/client/sdk.ts | 29 ++++++++++++++- packages/solidstart/test/client/sdk.test.ts | 41 +++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/packages/solidstart/src/client/sdk.ts b/packages/solidstart/src/client/sdk.ts index f44a2134ce50..7a8648bb5b11 100644 --- a/packages/solidstart/src/client/sdk.ts +++ b/packages/solidstart/src/client/sdk.ts @@ -1,13 +1,21 @@ import { applySdkMetadata } from '@sentry/core'; import type { BrowserOptions } from '@sentry/solid'; -import { init as initSolidSDK } from '@sentry/solid'; -import type { Client } from '@sentry/types'; +import { + browserTracingIntegration, + getDefaultIntegrations as getDefaultSolidIntegrations, + init as initSolidSDK, +} from '@sentry/solid'; +import type { Client, Integration } from '@sentry/types'; + +// Treeshakable guard to remove all code related to tracing +declare const __SENTRY_TRACING__: boolean; /** * Initializes the client side of the Solid Start SDK. */ export function init(options: BrowserOptions): Client | undefined { const opts = { + defaultIntegrations: getDefaultIntegrations(options), ...options, }; @@ -15,3 +23,20 @@ export function init(options: BrowserOptions): Client | undefined { return initSolidSDK(opts); } + +function getDefaultIntegrations(options: BrowserOptions): Integration[] { + // This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", + // in which case everything inside will get tree-shaken away + const integrations = getDefaultSolidIntegrations(options); + + if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) { + // We add the default BrowserTracingIntegration here always. + // We can do this, even if `solidRouterBrowserTracingIntegration` is + // supplied as integration in `init` by users because it will win + // over the default integration by virtue of having the same + // `BrowserTracing` integration name and being added later. + integrations.push(browserTracingIntegration()); + } + + return integrations; +} diff --git a/packages/solidstart/test/client/sdk.test.ts b/packages/solidstart/test/client/sdk.test.ts index 886bb29b515d..c92ca55fa4d8 100644 --- a/packages/solidstart/test/client/sdk.test.ts +++ b/packages/solidstart/test/client/sdk.test.ts @@ -3,6 +3,7 @@ import * as SentrySolid from '@sentry/solid'; import { vi } from 'vitest'; import { init as solidStartInit } from '../../src/client'; +import { solidRouterBrowserTracingIntegration } from '../../src/client/solidrouter'; const browserInit = vi.spyOn(SentrySolid, 'init'); @@ -34,3 +35,43 @@ describe('Initialize Solid Start SDK', () => { expect(browserInit).toHaveBeenLastCalledWith(expect.objectContaining(expectedMetadata)); }); }); + +describe('browserTracingIntegration', () => { + it('adds the `browserTracingIntegration` when `__SENTRY_TRACING__` is not set', () => { + const client = solidStartInit({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + }); + + const browserTracingIntegration = client?.getOptions().integrations.find(integration => integration.name === 'BrowserTracing'); + expect(browserTracingIntegration).toBeDefined(); + expect(browserTracingIntegration!.isDefaultInstance).toEqual(true); + }); + + it("doesn't add the `browserTracingIntegration` if `__SENTRY_TRACING__` is false", () => { + // @ts-expect-error Test setup for build-time flag + globalThis.__SENTRY_TRACING__ = false; + + const client = solidStartInit({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + }); + + const browserTracingIntegration = client?.getOptions().integrations.find(integration => integration.name === 'BrowserTracing'); + expect(browserTracingIntegration).toBeUndefined(); + + // @ts-expect-error Test setup for build-time flag + delete globalThis.__SENTRY_TRACING__; + }); + + it("doesn't add the default `browserTracingIntegration` if `solidBrowserTracingIntegration` was already passed in", () => { + const client = solidStartInit({ + integrations: [ + solidRouterBrowserTracingIntegration(), + ], + dsn: 'https://public@dsn.ingest.sentry.io/1337', + }); + + const browserTracingIntegration = client?.getOptions().integrations.find(integration => integration.name === 'BrowserTracing'); + expect(browserTracingIntegration).toBeDefined(); + expect(browserTracingIntegration!.isDefaultInstance).toBeUndefined(); + }) +});