Skip to content

Commit 2bd0505

Browse files
committed
fixup! fix(nextjs): Skip init inside a request of @sentry/cloudflare
1 parent 94224ee commit 2bd0505

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

‎packages/nextjs/src/server/index.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// can be removed once following issue is fixed: https://github.com/import-js/eslint-plugin-import/issues/703
33
/* eslint-disable import/export */
44
import { HTTP_TARGET, URL_QUERY } from '@sentry/conventions/attributes';
5-
import type { EventProcessor } from '@sentry/core';
5+
import type { EventProcessor, Scope } from '@sentry/core';
66
import {
77
_INTERNAL_getActiveClient,
88
applySdkMetadata,
@@ -237,9 +237,16 @@ export function init(options: NodeOptions): NodeClient | undefined {
237237

238238
// On the client, not the global scope, so a later `init()` after
239239
// `close()` does not stack another copy. In a request of `withSentry` on
240-
// Workers, `init` runs once per isolate and creates no client, so the global
241-
// scope gets them and reaches every client of `withSentry`.
242-
const eventProcessorTarget = isOwnedByCloudflare ? getGlobalScope() : client;
240+
// Workers, `init` creates no client, so the global scope gets them, which
241+
// reaches every client of `withSentry`, unless an earlier `init` added them.
242+
let eventProcessorTarget: NodeClient | Scope | undefined = client;
243+
if (isOwnedByCloudflare) {
244+
const globalScope = getGlobalScope();
245+
const isAdded = globalScope
246+
.getScopeData()
247+
.eventProcessors.some(processor => processor.id === 'DropReactControlFlowErrors');
248+
eventProcessorTarget = isAdded ? undefined : globalScope;
249+
}
243250
eventProcessorTarget?.addEventProcessor(
244251
Object.assign(
245252
((event, hint) => {

‎packages/nextjs/test/serverSdk.test.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,39 @@ describe('Server init()', () => {
9494
expect(second).toBe(first);
9595
});
9696

97-
it('skips init and sets the `turbopack` tag on Cloudflare Workers in a request of `withSentry` from `@sentry/cloudflare`', () => {
97+
it('skips init on Cloudflare Workers in a request of `withSentry` from `@sentry/cloudflare`', () => {
9898
vi.stubGlobal('navigator', { userAgent: 'Cloudflare-Workers' });
9999
setAsyncLocalStorageAsyncContextStrategy();
100-
(process as { turbopack?: boolean }).turbopack = true;
101100

102101
withIsolationScope(() => init({}));
103102

104103
expect(nodeInit).not.toHaveBeenCalled();
105-
expect(getGlobalScope().getScopeData().tags.turbopack).toBe(true);
106104
});
107105

108-
it('adds its event processors to the global scope on Cloudflare Workers in a request of `withSentry` from `@sentry/cloudflare`', () => {
106+
it('sets the `turbopack` tag on Cloudflare Workers in a request of `withSentry` from `@sentry/cloudflare`', () => {
109107
vi.stubGlobal('navigator', { userAgent: 'Cloudflare-Workers' });
110108
setAsyncLocalStorageAsyncContextStrategy();
109+
(process as { turbopack?: boolean }).turbopack = true;
111110

112111
withIsolationScope(() => init({}));
113112

113+
expect(getGlobalScope().getScopeData().tags.turbopack).toBe(true);
114+
});
115+
116+
it('adds its event processors to the global scope once on Cloudflare Workers in a request of `withSentry` from `@sentry/cloudflare`', () => {
117+
vi.stubGlobal('navigator', { userAgent: 'Cloudflare-Workers' });
118+
setAsyncLocalStorageAsyncContextStrategy();
119+
120+
withIsolationScope(() => {
121+
init({});
122+
init({});
123+
});
124+
114125
expect(
115126
getGlobalScope()
116127
.getScopeData()
117-
.eventProcessors.map(processor => processor.id),
118-
).toContain('DropReactControlFlowErrors');
128+
.eventProcessors.filter(processor => processor.id === 'DropReactControlFlowErrors'),
129+
).toHaveLength(1);
119130
});
120131

121132
it('inits on Cloudflare Workers outside of a request of `withSentry` from `@sentry/cloudflare`', () => {

0 commit comments

Comments
 (0)