diff --git a/packages/browser/src/integrations/useragent.ts b/packages/browser/src/integrations/useragent.ts index 925255f074e9..17e06a6789fc 100644 --- a/packages/browser/src/integrations/useragent.ts +++ b/packages/browser/src/integrations/useragent.ts @@ -1,5 +1,4 @@ -import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core'; -import { Event, Integration } from '@sentry/types'; +import { Event, EventProcessor, Hub, Integration } from '@sentry/types'; import { getGlobalObject } from '@sentry/utils'; const global = getGlobalObject(); @@ -19,25 +18,35 @@ export class UserAgent implements Integration { /** * @inheritDoc */ - public setupOnce(): void { + public setupOnce(addGlobalEventProcessor: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void { addGlobalEventProcessor((event: Event) => { if (getCurrentHub().getIntegration(UserAgent)) { + const { navigator, location, document } = global; // if none of the information we want exists, don't bother - if (!global.navigator && !global.location && !global.document) { + if (!navigator && !location && !document) { return event; } - // grab as much info as exists and add it to the event - const url = event.request?.url || global.location?.href; - const { referrer } = global.document || {}; - const { userAgent } = global.navigator || {}; - - const headers = { - ...event.request?.headers, - ...(referrer && { Referer: referrer }), - ...(userAgent && { 'User-Agent': userAgent }), - }; - const request = { ...(url && { url }), headers }; + const req = event.request; + const headers = req && req.headers ? { ...req.headers } : {}; + + const { userAgent } = navigator || {}; + if (userAgent) { + headers['User-Agent'] = userAgent; + } + + const { referrer } = document || {}; + if (referrer) { + headers.Referer = referrer; + } + + const request = { headers } as Record; + + const { href } = location || {}; + const url = (req && req.url) || href; + if (url) { + request.url = url; + } return { ...event, request }; }