Skip to content

Commit 3220902

Browse files
authored
ref(svelte): Remove SvelteKit detection (#15313)
When we first created the Svelte SDK, we wanted to measure how often it was used in a SvelteKit application. Therefore we added some SvelteKit detection logic to annotate events coming from the Svelte SDK. Since we have a SvelteKit SDK now, we don't need to track this anymore. Saves a few bytes, too. This patch therefore removes the detection and event processing logic
1 parent 2c76868 commit 3220902

File tree

2 files changed

+4
-108
lines changed

2 files changed

+4
-108
lines changed

packages/svelte/src/sdk.ts

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { BrowserOptions } from '@sentry/browser';
2-
import { WINDOW } from '@sentry/browser';
3-
import { addEventProcessor, init as browserInit } from '@sentry/browser';
4-
import type { Client, EventProcessor } from '@sentry/core';
2+
import { init as browserInit } from '@sentry/browser';
3+
import type { Client } from '@sentry/core';
54
import { applySdkMetadata } from '@sentry/core';
65
/**
76
* Inits the Svelte SDK
@@ -13,48 +12,5 @@ export function init(options: BrowserOptions): Client | undefined {
1312

1413
applySdkMetadata(opts, 'svelte');
1514

16-
const client = browserInit(opts);
17-
18-
detectAndReportSvelteKit();
19-
20-
return client;
21-
}
22-
23-
/**
24-
* Adds a global event processor to detect if the SDK is initialized in a SvelteKit frontend,
25-
* in which case we add SvelteKit an event.modules entry to outgoing events.
26-
* SvelteKit detection is performed only once, when the event processor is called for the
27-
* first time. We cannot perform this check upfront (directly when init is called) because
28-
* at this time, the HTML element might not yet be accessible.
29-
*/
30-
export function detectAndReportSvelteKit(): void {
31-
let detectedSvelteKit: boolean | undefined = undefined;
32-
33-
const svelteKitProcessor: EventProcessor = event => {
34-
if (detectedSvelteKit === undefined) {
35-
detectedSvelteKit = isSvelteKitApp();
36-
}
37-
if (detectedSvelteKit) {
38-
event.modules = {
39-
svelteKit: 'latest',
40-
...event.modules,
41-
};
42-
}
43-
return event;
44-
};
45-
svelteKitProcessor.id = 'svelteKitProcessor';
46-
47-
addEventProcessor(svelteKitProcessor);
48-
}
49-
50-
/**
51-
* To actually detect a SvelteKit frontend, we search the DOM for a special
52-
* div that's inserted by SvelteKit when the page is rendered. It's identified
53-
* by its id, 'svelte-announcer', and it's used to improve page accessibility.
54-
* This div is not present when only using Svelte without SvelteKit.
55-
*
56-
* @see https://github.com/sveltejs/kit/issues/307 for more information
57-
*/
58-
export function isSvelteKitApp(): boolean {
59-
return !!WINDOW.document.querySelector('div#svelte-announcer');
15+
return browserInit(opts);
6016
}

packages/svelte/test/sdk.test.ts

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
66

77
import { SDK_VERSION } from '@sentry/browser';
88
import * as SentryBrowser from '@sentry/browser';
9-
import type { EventProcessor } from '@sentry/core';
109

11-
import { detectAndReportSvelteKit, init as svelteInit, isSvelteKitApp } from '../src/sdk';
12-
13-
let passedEventProcessor: EventProcessor | undefined;
10+
import { init as svelteInit } from '../src/sdk';
1411

1512
const browserInit = vi.spyOn(SentryBrowser, 'init');
16-
const addEventProcessor = vi
17-
.spyOn(SentryBrowser, 'addEventProcessor')
18-
.mockImplementation((eventProcessor: EventProcessor) => {
19-
passedEventProcessor = eventProcessor;
20-
return () => {};
21-
});
2213

2314
describe('Initialize Svelte SDk', () => {
2415
beforeEach(() => {
@@ -84,54 +75,3 @@ describe('Initialize Svelte SDk', () => {
8475
expect(client).not.toBeUndefined();
8576
});
8677
});
87-
88-
describe('detectAndReportSvelteKit()', () => {
89-
const originalHtmlBody = document.body.innerHTML;
90-
beforeEach(() => {
91-
vi.clearAllMocks();
92-
document.body.innerHTML = originalHtmlBody;
93-
passedEventProcessor = undefined;
94-
});
95-
96-
it('registers an event processor', async () => {
97-
detectAndReportSvelteKit();
98-
99-
expect(addEventProcessor).toHaveBeenCalledTimes(1);
100-
expect(passedEventProcessor?.id).toEqual('svelteKitProcessor');
101-
});
102-
103-
it('adds "SvelteKit" as a module to the event, if SvelteKit was detected', () => {
104-
document.body.innerHTML += '<div id="svelte-announcer">Home</div>';
105-
detectAndReportSvelteKit();
106-
107-
const processedEvent = passedEventProcessor?.({} as unknown as any, {});
108-
109-
expect(processedEvent).toBeDefined();
110-
expect(processedEvent).toEqual({ modules: { svelteKit: 'latest' } });
111-
});
112-
113-
it("doesn't add anything to the event, if SvelteKit was not detected", () => {
114-
document.body.innerHTML = '';
115-
detectAndReportSvelteKit();
116-
117-
const processedEvent = passedEventProcessor?.({} as unknown as any, {});
118-
119-
expect(processedEvent).toBeDefined();
120-
expect(processedEvent).toEqual({});
121-
});
122-
123-
describe('isSvelteKitApp()', () => {
124-
it('returns true if the svelte-announcer div is present', () => {
125-
document.body.innerHTML += '<div id="svelte-announcer">Home</div>';
126-
expect(isSvelteKitApp()).toBe(true);
127-
});
128-
it('returns false if the svelte-announcer div is not present (but similar elements)', () => {
129-
document.body.innerHTML += '<div id="svelte-something">Home</div>';
130-
expect(isSvelteKitApp()).toBe(false);
131-
});
132-
it('returns false if no div is present', () => {
133-
document.body.innerHTML = '';
134-
expect(isSvelteKitApp()).toBe(false);
135-
});
136-
});
137-
});

0 commit comments

Comments
 (0)