Skip to content

Commit 99aac21

Browse files
committed
Deduplicate basename stripping, remove unused export
1 parent 8564b40 commit 99aac21

File tree

3 files changed

+29
-42
lines changed

3 files changed

+29
-42
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,3 @@ export {
3535

3636
// Lazy route exports
3737
export { createAsyncHandlerProxy, handleAsyncHandlerResult, checkRouteForAsyncHandler } from './lazy-routes';
38-
39-
// Route manifest exports
40-
export { matchRouteManifest } from './route-manifest';

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
import { debug } from '@sentry/core';
22
import { DEBUG_BUILD } from '../debug-build';
33

4+
/**
5+
* Strip the basename from a pathname if exists.
6+
*
7+
* Vendored and modified from `react-router`
8+
* https://github.com/remix-run/react-router/blob/462bb712156a3f739d6139a0f14810b76b002df6/packages/router/utils.ts#L1038
9+
*/
10+
export function stripBasenameFromPathname(pathname: string, basename: string): string {
11+
if (!basename || basename === '/') {
12+
return pathname;
13+
}
14+
15+
if (!pathname.toLowerCase().startsWith(basename.toLowerCase())) {
16+
return pathname;
17+
}
18+
19+
// We want to leave trailing slash behavior in the user's control, so if they
20+
// specify a basename with a trailing slash, we should support it
21+
const startIndex = basename.endsWith('/') ? basename.length - 1 : basename.length;
22+
const nextChar = pathname.charAt(startIndex);
23+
if (nextChar && nextChar !== '/') {
24+
// pathname does not start with basename/
25+
return pathname;
26+
}
27+
28+
return pathname.slice(startIndex) || '/';
29+
}
30+
431
// Cache for sorted manifests - keyed by manifest array reference
532
const SORTED_MANIFEST_CACHE = new WeakMap<string[], string[]>();
633

@@ -13,17 +40,7 @@ export function matchRouteManifest(pathname: string, manifest: string[], basenam
1340
return null;
1441
}
1542

16-
let normalizedPathname = pathname;
17-
if (basename && basename !== '/') {
18-
const base = basename.endsWith('/') ? basename.slice(0, -1) : basename;
19-
if (normalizedPathname.toLowerCase().startsWith(base.toLowerCase())) {
20-
// Verify basename ends at segment boundary (followed by / or end of string)
21-
const nextChar = normalizedPathname.charAt(base.length);
22-
if (nextChar === '/' || nextChar === '') {
23-
normalizedPathname = normalizedPathname.slice(base.length) || '/';
24-
}
25-
}
26-
}
43+
const normalizedPathname = basename ? stripBasenameFromPathname(pathname, basename) : pathname;
2744

2845
let sorted = SORTED_MANIFEST_CACHE.get(manifest);
2946
if (!sorted) {

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

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Span, TransactionSource } from '@sentry/core';
22
import { debug, getActiveSpan, getRootSpan, spanToJSON } from '@sentry/core';
33
import { DEBUG_BUILD } from '../debug-build';
44
import type { Location, MatchRoutes, RouteMatch, RouteObject } from '../types';
5-
import { matchRouteManifest } from './route-manifest';
5+
import { matchRouteManifest, stripBasenameFromPathname } from './route-manifest';
66

77
// Global variables that these utilities depend on
88
let _matchRoutes: MatchRoutes;
@@ -135,33 +135,6 @@ export function getNumberOfUrlSegments(url: string): number {
135135
return url.split(/\\?\//).filter(s => s.length > 0 && s !== ',').length;
136136
}
137137

138-
/**
139-
* Strip the basename from a pathname if exists.
140-
*
141-
* Vendored and modified from `react-router`
142-
* https://github.com/remix-run/react-router/blob/462bb712156a3f739d6139a0f14810b76b002df6/packages/router/utils.ts#L1038
143-
*/
144-
function stripBasenameFromPathname(pathname: string, basename: string): string {
145-
if (!basename || basename === '/') {
146-
return pathname;
147-
}
148-
149-
if (!pathname.toLowerCase().startsWith(basename.toLowerCase())) {
150-
return pathname;
151-
}
152-
153-
// We want to leave trailing slash behavior in the user's control, so if they
154-
// specify a basename with a trailing slash, we should support it
155-
const startIndex = basename.endsWith('/') ? basename.length - 1 : basename.length;
156-
const nextChar = pathname.charAt(startIndex);
157-
if (nextChar && nextChar !== '/') {
158-
// pathname does not start with basename/
159-
return pathname;
160-
}
161-
162-
return pathname.slice(startIndex) || '/';
163-
}
164-
165138
// Exported utility functions
166139

167140
/**

0 commit comments

Comments
 (0)