Skip to content

Commit 987bff8

Browse files
authored
feat(react)!: Remove deprecated getNumberOfUrlSegments method (#14744)
Removes code deprecated in #14458 Removes `getNumberOfUrlSegments` as a public export from `@sentry/core`. This method is still used, so we extract it into a helper in `@sentry/react` (and port the tests accordingly).
1 parent d2b23d7 commit 987bff8

File tree

7 files changed

+26
-39
lines changed

7 files changed

+26
-39
lines changed

docs/migration/v8-to-v9.md

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ It will be removed in a future major version.
9797
- The `wrapUseRoutes` method has been removed. Use `wrapUseRoutesV6` or `wrapUseRoutesV7` instead depending on what version of react router you are using.
9898
- The `wrapCreateBrowserRouter` method has been removed. Use `wrapCreateBrowserRouterV6` or `wrapCreateBrowserRouterV7` depending on what version of react router you are using.
9999

100+
### `@sentry/core`
101+
102+
- The `getNumberOfUrlSegments` method has been removed. There are no replacements.
103+
100104
## 5. Build Changes
101105

102106
Previously the CJS versions of the SDK code (wrongfully) contained compatibility statements for default exports in ESM:

packages/core/src/utils-hoist/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ export {
162162
parseBaggageHeader,
163163
} from './baggage';
164164

165-
// eslint-disable-next-line deprecation/deprecation
166-
export { getNumberOfUrlSegments, getSanitizedUrlString, parseUrl, stripUrlQueryAndFragment } from './url';
165+
export { getSanitizedUrlString, parseUrl, stripUrlQueryAndFragment } from './url';
167166
// eslint-disable-next-line deprecation/deprecation
168167
export { makeFifoCache } from './cache';
169168
export { eventFromMessage, eventFromUnknownInput, exceptionFromError, parseStackFrames } from './eventbuilder';

packages/core/src/utils-hoist/url.ts

-11
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ export function stripUrlQueryAndFragment(urlPath: string): string {
4848
return (urlPath.split(/[?#]/, 1) as [string, ...string[]])[0];
4949
}
5050

51-
/**
52-
* Returns number of URL segments of a passed string URL.
53-
*
54-
* @deprecated This function will be removed in the next major version.
55-
*/
56-
// TODO(v9): Hoist this function into the places where we use it. (as it stands only react router v6 instrumentation)
57-
export function getNumberOfUrlSegments(url: string): number {
58-
// split at '/' or at '\/' to split regex urls correctly
59-
return url.split(/\\?\//).filter(s => s.length > 0 && s !== ',').length;
60-
}
61-
6251
/**
6352
* Takes a URL object and returns a sanitized string which is safe to use as span name
6453
* see: https://develop.sentry.dev/sdk/data-handling/#structuring-data

packages/core/test/utils-hoist/url.test.ts

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
getNumberOfUrlSegments,
3-
getSanitizedUrlString,
4-
parseUrl,
5-
stripUrlQueryAndFragment,
6-
} from '../../src/utils-hoist/url';
1+
import { getSanitizedUrlString, parseUrl, stripUrlQueryAndFragment } from '../../src/utils-hoist/url';
72

83
describe('stripQueryStringAndFragment', () => {
94
const urlString = 'http://dogs.are.great:1231/yay/';
@@ -26,18 +21,6 @@ describe('stripQueryStringAndFragment', () => {
2621
});
2722
});
2823

29-
describe('getNumberOfUrlSegments', () => {
30-
test.each([
31-
['regular path', '/projects/123/views/234', 4],
32-
['single param parameterized path', '/users/:id/details', 3],
33-
['multi param parameterized path', '/stores/:storeId/products/:productId', 4],
34-
['regex path', String(/\/api\/post[0-9]/), 2],
35-
])('%s', (_: string, input, output) => {
36-
// eslint-disable-next-line deprecation/deprecation
37-
expect(getNumberOfUrlSegments(input)).toEqual(output);
38-
});
39-
});
40-
4124
describe('getSanitizedUrlString', () => {
4225
it.each([
4326
['regular url', 'https://somedomain.com', 'https://somedomain.com'],

packages/react/src/reactrouterv6-compat-utils.tsx

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
getActiveSpan,
1717
getClient,
1818
getCurrentScope,
19-
getNumberOfUrlSegments,
2019
getRootSpan,
2120
logger,
2221
spanToJSON,
@@ -436,8 +435,6 @@ function getNormalizedName(
436435
// If the route defined on the element is something like
437436
// <Route path="/stores/:storeId/products/:productId" element={<div>Product</div>} />
438437
// We should check against the branch.pathname for the number of / separators
439-
// TODO(v9): Put the implementation of `getNumberOfUrlSegments` in this file
440-
// eslint-disable-next-line deprecation/deprecation
441438
getNumberOfUrlSegments(pathBuilder) !== getNumberOfUrlSegments(branch.pathname) &&
442439
// We should not count wildcard operators in the url segments calculation
443440
!pathEndsWithWildcard(pathBuilder)
@@ -572,3 +569,11 @@ function getActiveRootSpan(): Span | undefined {
572569
// Only use this root span if it is a pageload or navigation span
573570
return op === 'navigation' || op === 'pageload' ? rootSpan : undefined;
574571
}
572+
573+
/**
574+
* Returns number of URL segments of a passed string URL.
575+
*/
576+
export function getNumberOfUrlSegments(url: string): number {
577+
// split at '/' or at '\/' to split regex urls correctly
578+
return url.split(/\\?\//).filter(s => s.length > 0 && s !== ',').length;
579+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getNumberOfUrlSegments } from '../src/reactrouterv6-compat-utils';
2+
3+
describe('getNumberOfUrlSegments', () => {
4+
test.each([
5+
['regular path', '/projects/123/views/234', 4],
6+
['single param parameterized path', '/users/:id/details', 3],
7+
['multi param parameterized path', '/stores/:storeId/products/:productId', 4],
8+
['regex path', String(/\/api\/post[0-9]/), 2],
9+
])('%s', (_: string, input, output) => {
10+
expect(getNumberOfUrlSegments(input)).toEqual(output);
11+
});
12+
});

packages/utils/src/index.ts

-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ import {
7777
getFunctionName as getFunctionName_imported,
7878
getGlobalSingleton as getGlobalSingleton_imported,
7979
getLocationHref as getLocationHref_imported,
80-
getNumberOfUrlSegments as getNumberOfUrlSegments_imported,
8180
getOriginalFunction as getOriginalFunction_imported,
8281
getSDKSource as getSDKSource_imported,
8382
getSanitizedUrlString as getSanitizedUrlString_imported,
@@ -644,10 +643,6 @@ export const addRequestDataToEvent = addRequestDataToEvent_imported;
644643
// eslint-disable-next-line deprecation/deprecation
645644
export const BAGGAGE_HEADER_NAME = BAGGAGE_HEADER_NAME_imported;
646645

647-
/** @deprecated Import from `@sentry/core` instead. */
648-
// eslint-disable-next-line deprecation/deprecation
649-
export const getNumberOfUrlSegments = getNumberOfUrlSegments_imported;
650-
651646
/** @deprecated Import from `@sentry/core` instead. */
652647
export const getSanitizedUrlString = getSanitizedUrlString_imported;
653648

0 commit comments

Comments
 (0)