Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/browser-utils/src/getLocationHref.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { WINDOW } from './types';

/**
* A safe form of location.href.
*/
export function getLocationHref(): string {
try {
return WINDOW.document?.location.href ?? '';
} catch {
return '';
}
}
2 changes: 2 additions & 0 deletions packages/browser-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export { interactionsIntegration } from './performance/interactions';

export { isBotUserAgent } from './isBotUserAgent';

export { getLocationHref } from './getLocationHref';

export { userTimingIntegration } from './performance/userTiming';

export { extractNetworkProtocol } from './performance/utils';
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
addExceptionTypeValue,
addNonEnumerableProperty,
captureException,
getLocationHref,
getOriginalFunction,
GLOBAL_OBJ,
markFunctionWrapped,
withScope,
} from '@sentry/core/browser';
import { getLocationHref } from '@sentry/browser-utils';

export const WINDOW = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;

Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/integrations/globalhandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
debug,
defineIntegration,
getClient,
getLocationHref,
isPrimitive,
isString,
stripDataUrlContent,
UNKNOWN_FUNCTION,
} from '@sentry/core/browser';
import type { BrowserClient } from '../client';
import { DEBUG_BUILD } from '../debug-build';
import { getLocationHref } from '@sentry/browser-utils';
import { eventFromUnknownInput } from '../eventbuilder';
import { shouldIgnoreOnError } from '../helpers';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
getClient,
getCurrentScope,
getDynamicSamplingContextFromSpan,
getLocationHref,
GLOBAL_OBJ,
hasSpansEnabled,
hasSpanStreamingEnabled,
Expand All @@ -41,6 +40,7 @@ import {
import {
addHistoryInstrumentationHandler,
addPerformanceEntries,
getLocationHref,
isBotUserAgent,
startTrackingLongAnimationFrames,
startTrackingLongTasks,
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/tracing/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
addFetchInstrumentationHandler,
getActiveSpan,
getClient,
getLocationHref,
getTraceData,
getUrlFragment,
getUrlQuery,
Expand All @@ -37,6 +36,7 @@ import { filterCollectedUrl, filterCollectedUrlQuery } from '@sentry/core';
import {
addPerformanceInstrumentationHandler,
addXhrInstrumentationHandler,
getLocationHref,
parseXhrResponseHeaders,
resourceTimingToSpanAttributes,
SENTRY_XHR_DATA_KEY,
Expand Down
3 changes: 2 additions & 1 deletion packages/browser/src/utils/detectBrowserExtension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { consoleSandbox, getLocationHref } from '@sentry/core/browser';
import { getLocationHref } from '@sentry/browser-utils';
import { consoleSandbox } from '@sentry/core/browser';
import { DEBUG_BUILD } from '../debug-build';
import { WINDOW } from '../helpers';

Expand Down
3 changes: 2 additions & 1 deletion packages/browser/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { afterEach, describe, expect, it, test, vi } from 'vitest';
import type { BrowserOptions } from '../src';
import { WINDOW } from '../src';
import { init } from '../src/sdk';
import * as browserUtils from '@sentry/browser-utils';

const PUBLIC_DSN = 'https://username@domain/123';

Expand Down Expand Up @@ -168,7 +169,7 @@ describe('init', () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});

const locationHrefSpy = vi
.spyOn(SentryCore, 'getLocationHref')
.spyOn(browserUtils, 'getLocationHref')
.mockImplementation(() => `${extensionProtocol}://mock-extension-id/dedicated-page.html`);

Object.defineProperty(WINDOW, 'browser', { value: { runtime: { id: 'mock-extension-id' } }, writable: true });
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/test/tracing/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('shouldAttachHeaders', () => {
let locationHrefSpy: MockInstance;

beforeEach(() => {
locationHrefSpy = vi.spyOn(utils, 'getLocationHref').mockImplementation(() => 'https://my-origin.com');
locationHrefSpy = vi.spyOn(browserUtils, 'getLocationHref').mockImplementation(() => 'https://my-origin.com');
});

afterEach(() => {
Expand Down Expand Up @@ -232,7 +232,7 @@ describe('shouldAttachHeaders', () => {

beforeEach(() => {
locationHrefSpy = vi
.spyOn(utils, 'getLocationHref')
.spyOn(browserUtils, 'getLocationHref')
.mockImplementation(() => 'https://my-origin.com/api/my-route');
});

Expand Down Expand Up @@ -364,7 +364,7 @@ describe('shouldAttachHeaders', () => {
let locationHrefSpy: MockInstance;

beforeEach(() => {
locationHrefSpy = vi.spyOn(utils, 'getLocationHref').mockImplementation(() => '');
locationHrefSpy = vi.spyOn(browserUtils, 'getLocationHref').mockImplementation(() => '');
});

afterEach(() => {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/browser-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export { startIdleSpan } from './tracing/idleSpan';

export { spanStreamingIntegration } from './integrations/browserSpanStreaming';

export { getLocationHref } from './utils/browser';
export { supportsDOMError, supportsHistory, supportsNativeFetch, supportsReportingObserver } from './utils/supports';
export type { XhrBreadcrumbData, XhrBreadcrumbHint } from './types/breadcrumb';
export type {
Expand Down
17 changes: 14 additions & 3 deletions packages/feedback/src/core/sendFeedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
SendFeedbackParams,
TransportMakeRequestResponse,
} from '@sentry/core';
import { captureFeedback, getClient, getCurrentScope, getLocationHref } from '@sentry/core';
import { FEEDBACK_API_SOURCE } from '../constants';
import { captureFeedback, getClient, getCurrentScope } from '@sentry/core';
import { FEEDBACK_API_SOURCE, WINDOW } from '../constants';
import { createFeedbackError } from '../util/createFeedbackError';

/**
Expand Down Expand Up @@ -36,7 +36,7 @@ export const sendFeedback: SendFeedback = (
const eventId = captureFeedback(
{
source: FEEDBACK_API_SOURCE,
url: getLocationHref(),
url: _getLocationHref(),
...params,
},
hint,
Expand Down Expand Up @@ -113,3 +113,14 @@ export const sendFeedback: SendFeedback = (
* },
* }
*/

/**
* A safe form of location.href.
*/
function _getLocationHref(): string {
try {
return WINDOW.document.location.href;
} catch {
return '';
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getLocationHref } from '@sentry/browser-utils';
import type { ErrorEvent, Event } from '@sentry/core';
import { getLocationHref } from '@sentry/core';
import type { ReplayContainer } from '../types';
import { createBreadcrumb } from '../util/createBreadcrumb';
import { isErrorEvent } from '../util/eventUtils';
Expand Down
Loading