Skip to content

Commit 453f5c6

Browse files
committed
Clean up more
1 parent c46ee84 commit 453f5c6

File tree

4 files changed

+9
-49
lines changed

4 files changed

+9
-49
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ export {
1717
// Utility exports
1818
export {
1919
initializeRouterUtils,
20-
getGlobalLocation,
21-
getGlobalPathname,
2220
prefixWithSlash,
2321
rebuildRoutePathFromAllRoutes,
2422
locationIsInsideDescendantRoute,

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
browserTracingIntegration,
77
startBrowserTracingNavigationSpan,
88
startBrowserTracingPageLoadSpan,
9+
WINDOW,
910
} from '@sentry/browser';
1011
import type { Client, Integration, Span, TransactionSource } from '@sentry/core';
1112
import {
@@ -41,8 +42,6 @@ import type {
4142
} from '../types';
4243
import { checkRouteForAsyncHandler } from './lazy-routes';
4344
import {
44-
getGlobalLocation,
45-
getGlobalPathname,
4645
getNormalizedName,
4746
initializeRouterUtils,
4847
locationIsInsideDescendantRoute,
@@ -138,7 +137,12 @@ export function processResolvedRoutes(
138137
// Try to use the provided location first, then fall back to global window location if needed
139138
let location = currentLocation;
140139
if (!location) {
141-
location = getGlobalLocation();
140+
if (typeof WINDOW !== 'undefined') {
141+
const globalLocation = WINDOW.location;
142+
if (globalLocation) {
143+
location = { pathname: globalLocation.pathname };
144+
}
145+
}
142146
}
143147

144148
if (location) {
@@ -417,7 +421,7 @@ export function createReactRouterV6CompatibleTracingIntegration(
417421
afterAllSetup(client) {
418422
integration.afterAllSetup(client);
419423

420-
const initPathName = getGlobalPathname();
424+
const initPathName = WINDOW.location?.pathname;
421425
if (instrumentPageLoad && initPathName) {
422426
startBrowserTracingPageLoadSpan(client, {
423427
name: initPathName,
@@ -550,7 +554,7 @@ function wrapPatchRoutesOnNavigation(
550554
if (activeRootSpan && (spanToJSON(activeRootSpan) as { op?: string }).op === 'navigation') {
551555
// For memory routers, we don't have a reliable way to get the current pathname
552556
// without accessing window.location, so we'll use targetPath for both cases
553-
const pathname = targetPath || (isMemoryRouter ? getGlobalPathname() : undefined);
557+
const pathname = targetPath || (isMemoryRouter ? WINDOW.location?.pathname : undefined);
554558
if (pathname) {
555559
updateNavigationSpan(
556560
activeRootSpan,

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

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

@@ -15,31 +14,6 @@ export function initializeRouterUtils(matchRoutes: MatchRoutes, stripBasename: b
1514
_stripBasename = stripBasename;
1615
}
1716

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-
32-
/**
33-
* Gets the pathname from the window object in browser environments.
34-
* Returns undefined if window is not available.
35-
*/
36-
export function getGlobalPathname(): string | undefined {
37-
if (typeof WINDOW !== 'undefined') {
38-
return WINDOW.location?.pathname;
39-
}
40-
return undefined;
41-
}
42-
4317
// Helper functions
4418
function pickPath(match: RouteMatch): string {
4519
return trimWildcard(match.route.path || '');

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest';
22
import {
3-
getGlobalLocation,
4-
getGlobalPathname,
53
getNormalizedName,
64
getNumberOfUrlSegments,
75
initializeRouterUtils,
@@ -59,20 +57,6 @@ describe('reactrouter-compat-utils/utils', () => {
5957
});
6058
});
6159

62-
describe('getGlobalLocation', () => {
63-
it('should return location with pathname when WINDOW is available', () => {
64-
const result = getGlobalLocation();
65-
expect(result).toEqual({ pathname: '/test/path' });
66-
});
67-
});
68-
69-
describe('getGlobalPathname', () => {
70-
it('should return pathname when WINDOW is available', () => {
71-
const result = getGlobalPathname();
72-
expect(result).toBe('/test/path');
73-
});
74-
});
75-
7660
describe('prefixWithSlash', () => {
7761
it('should add slash to string without leading slash', () => {
7862
expect(prefixWithSlash('path')).toBe('/path');

0 commit comments

Comments
 (0)