Skip to content

Commit c46b804

Browse files
committed
fix(sveltekit): Ensure trace meta tags are always injected
1 parent 05684d4 commit c46b804

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

packages/sveltekit/src/server/handle.ts

+8-19
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
withIsolationScope,
1212
} from '@sentry/core';
1313
import { startSpan } from '@sentry/core';
14-
import { captureException, continueTrace } from '@sentry/node';
14+
import { captureException, continueTrace, getTraceMetaTags } from '@sentry/node';
1515
import type { Span } from '@sentry/types';
1616
import {
1717
dynamicSamplingContextToSentryBaggageHeader,
@@ -113,25 +113,14 @@ export function addSentryCodeToPage(options: SentryHandleOptions): NonNullable<R
113113
const nonce = fetchProxyScriptNonce ? `nonce="${fetchProxyScriptNonce}"` : '';
114114

115115
return ({ html }) => {
116-
const activeSpan = getActiveSpan();
117-
const rootSpan = activeSpan ? getRootSpan(activeSpan) : undefined;
118-
if (rootSpan) {
119-
const traceparentData = spanToTraceHeader(rootSpan);
120-
const dynamicSamplingContext = dynamicSamplingContextToSentryBaggageHeader(
121-
getDynamicSamplingContextFromSpan(rootSpan),
122-
);
123-
const contentMeta = `<head>
124-
<meta name="sentry-trace" content="${traceparentData}"/>
125-
<meta name="baggage" content="${dynamicSamplingContext}"/>
126-
`;
127-
const contentScript = shouldInjectScript ? `<script ${nonce}>${FETCH_PROXY_SCRIPT}</script>` : '';
128-
129-
const content = `${contentMeta}\n${contentScript}`;
130-
131-
return html.replace('<head>', content);
132-
}
116+
const metaTags = getTraceMetaTags();
117+
const headWithMetaTags = metaTags ? `<head>\n${metaTags}` : '<head>';
118+
119+
const headWithFetchScript = shouldInjectScript ? `\n<script ${nonce}>${FETCH_PROXY_SCRIPT}</script>` : '';
120+
121+
const modifiedHead = `${headWithMetaTags}${headWithFetchScript}`;
133122

134-
return html;
123+
return html.replace('<head>', modifiedHead);
135124
};
136125
}
137126

packages/sveltekit/test/server/handle.test.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,14 @@ describe('addSentryCodeToPage', () => {
429429
</body>
430430
</html>`;
431431

432-
it('does not add meta tags if no active transaction', () => {
432+
it("Adds add meta tags and fetch proxy script if there's no active transaction", () => {
433433
const transformPageChunk = addSentryCodeToPage({});
434434
const transformed = transformPageChunk({ html, done: true });
435-
expect(transformed).toEqual(html);
435+
436+
expect(transformed).toContain('<meta name="sentry-trace"');
437+
expect(transformed).toContain('<meta name="baggage"');
438+
expect(transformed).not.toContain('sentry-transaction=');
439+
expect(transformed).toContain(`<script >${FETCH_PROXY_SCRIPT}</script>`);
436440
});
437441

438442
it('adds meta tags and the fetch proxy script if there is an active transaction', () => {
@@ -442,6 +446,7 @@ describe('addSentryCodeToPage', () => {
442446

443447
expect(transformed).toContain('<meta name="sentry-trace"');
444448
expect(transformed).toContain('<meta name="baggage"');
449+
expect(transformed).toContain('sentry-transaction=test');
445450
expect(transformed).toContain(`<script >${FETCH_PROXY_SCRIPT}</script>`);
446451
});
447452
});
@@ -453,18 +458,17 @@ describe('addSentryCodeToPage', () => {
453458

454459
expect(transformed).toContain('<meta name="sentry-trace"');
455460
expect(transformed).toContain('<meta name="baggage"');
461+
expect(transformed).toContain('sentry-transaction=test');
456462
expect(transformed).toContain(`<script nonce="123abc">${FETCH_PROXY_SCRIPT}</script>`);
457463
});
458464
});
459465

460466
it('does not add the fetch proxy script if the `injectFetchProxyScript` option is false', () => {
461467
const transformPageChunk = addSentryCodeToPage({ injectFetchProxyScript: false });
462-
SentryNode.startSpan({ name: 'test' }, () => {
463-
const transformed = transformPageChunk({ html, done: true }) as string;
468+
const transformed = transformPageChunk({ html, done: true }) as string;
464469

465-
expect(transformed).toContain('<meta name="sentry-trace"');
466-
expect(transformed).toContain('<meta name="baggage"');
467-
expect(transformed).not.toContain(`<script >${FETCH_PROXY_SCRIPT}</script>`);
468-
});
470+
expect(transformed).toContain('<meta name="sentry-trace"');
471+
expect(transformed).toContain('<meta name="baggage"');
472+
expect(transformed).not.toContain(`<script >${FETCH_PROXY_SCRIPT}</script>`);
469473
});
470474
});

0 commit comments

Comments
 (0)