Skip to content

Commit ad11220

Browse files
committed
Move getGlobalLocation to utils
1 parent 1bff156 commit ad11220

File tree

3 files changed

+19
-28
lines changed

3 files changed

+19
-28
lines changed

packages/react/src/reactrouter-compat-utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export {
1616
// Utility exports
1717
export {
1818
initializeRouterUtils,
19+
getGlobalLocation,
1920
prefixWithSlash,
2021
rebuildRoutePathFromAllRoutes,
2122
locationIsInsideDescendantRoute,

packages/react/src/reactrouter-compat-utils/instrumentation.tsx

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
browserTracingIntegration,
77
startBrowserTracingNavigationSpan,
88
startBrowserTracingPageLoadSpan,
9-
WINDOW,
109
} from '@sentry/browser';
1110
import type { Client, Integration, Span, TransactionSource } from '@sentry/core';
1211
import {
@@ -42,6 +41,7 @@ import type {
4241
} from '../types';
4342
import { checkRouteForAsyncHandler, updateNavigationSpanWithLazyRoutes } from './lazy-routes';
4443
import {
44+
getGlobalLocation,
4545
getNormalizedName,
4646
initializeRouterUtils,
4747
locationIsInsideDescendantRoute,
@@ -59,31 +59,6 @@ let _enableAsyncRouteHandlers: boolean = false;
5959

6060
const CLIENTS_WITH_INSTRUMENT_NAVIGATION = new WeakSet<Client>();
6161

62-
/**
63-
* Gets the current location from the window object in browser environments.
64-
* Returns undefined if window is not available.
65-
*/
66-
function getGlobalLocation(): Location | undefined {
67-
if (typeof WINDOW !== 'undefined') {
68-
const globalLocation = WINDOW.location;
69-
if (globalLocation) {
70-
return { pathname: globalLocation.pathname };
71-
}
72-
}
73-
return undefined;
74-
}
75-
76-
/**
77-
* Gets the pathname from the window object in browser environments.
78-
* Returns undefined if window is not available.
79-
*/
80-
function getGlobalPathname(): string | undefined {
81-
if (typeof WINDOW !== 'undefined') {
82-
return WINDOW.location?.pathname;
83-
}
84-
return undefined;
85-
}
86-
8762
export interface ReactRouterOptions {
8863
useEffect: UseEffect;
8964
useLocation: UseLocation;
@@ -213,7 +188,7 @@ function wrapPatchRoutesOnNavigation(
213188
if (activeRootSpan && (spanToJSON(activeRootSpan) as { op?: string }).op === 'navigation') {
214189
// For memory routers, we don't have a reliable way to get the current pathname
215190
// without accessing window.location, so we'll use targetPath for both cases
216-
const pathname = targetPath || (isMemoryRouter ? getGlobalPathname() : undefined);
191+
const pathname = targetPath || (isMemoryRouter ? getGlobalLocation()?.pathname : undefined);
217192
if (pathname) {
218193
updateNavigationSpanWithLazyRoutes(
219194
activeRootSpan,
@@ -446,7 +421,7 @@ export function createReactRouterV6CompatibleTracingIntegration(
446421
afterAllSetup(client) {
447422
integration.afterAllSetup(client);
448423

449-
const initPathName = getGlobalPathname();
424+
const initPathName = getGlobalLocation()?.pathname;
450425
if (instrumentPageLoad && initPathName) {
451426
startBrowserTracingPageLoadSpan(client, {
452427
name: initPathName,

packages/react/src/reactrouter-compat-utils/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { WINDOW } from '@sentry/browser';
12
import type { TransactionSource } from '@sentry/core';
23
import type { Location, MatchRoutes, RouteMatch, RouteObject } from '../types';
34

@@ -14,6 +15,20 @@ export function initializeRouterUtils(matchRoutes: MatchRoutes, stripBasename: b
1415
_stripBasename = stripBasename;
1516
}
1617

18+
/**
19+
* Gets the current location from the window object in browser environments.
20+
* Returns undefined if window is not available.
21+
*/
22+
export function getGlobalLocation(): Location | undefined {
23+
if (typeof WINDOW !== 'undefined') {
24+
const globalLocation = WINDOW.location;
25+
if (globalLocation) {
26+
return { pathname: globalLocation.pathname };
27+
}
28+
}
29+
return undefined;
30+
}
31+
1732
// Helper functions
1833
function pickPath(match: RouteMatch): string {
1934
return trimWildcard(match.route.path || '');

0 commit comments

Comments
 (0)