Description
Now that we support ES2020 (aka not IE11 anymore) and Node.js 18+, we can get rid of parseUrl in favor of a method that just uses the built-in URL object. This will save us some bundle size (given we can remove that regex), and we get performance benefits from using native code.
|
export function parseUrl(url: string): PartialURL { |
Instead of just blanket replacing parseUrl, we'll slowly convert all it's usages to using a new helper, which looks something like so:
/**
* Parses string to a URL object
*
* @param url - The URL to parse
* @returns The parsed URL object or undefined if the URL is invalid
*/
export function parseStringToURL(url: string): URL | undefined {
try {
// Node 20+, Chrome 120+, Firefox 115+, Safari 17+
if ('canParse' in URL) {
// Use `canParse` to short-circuit the URL constructor if it's not a valid URL
// This is faster than trying to construct the URL and catching the error
return (URL as unknown as URLwithCanParse).canParse(url) ? new URL(url) : undefined;
}
} catch {
// empty body
}
return undefined;
}
With that, we can also refactor getSanitizedUrlString, which should provide some performance benefits.
|
export function getSanitizedUrlString(url: PartialURL): string { |
Reminder list:
- Can we remove
extractQueryParamsFromUrl?
Description
Now that we support ES2020 (aka not IE11 anymore) and Node.js 18+, we can get rid of
parseUrlin favor of a method that just uses the built-in URL object. This will save us some bundle size (given we can remove that regex), and we get performance benefits from using native code.sentry-javascript/packages/core/src/utils-hoist/url.ts
Line 17 in 3d63621
Instead of just blanket replacing
parseUrl, we'll slowly convert all it's usages to using a new helper, which looks something like so:@sentry/core, and implementparseStringToURLsentry-javascript/packages/browser-utils/src/instrument/xhr.ts
Line 150 in 3d63621
sentry-javascript/packages/browser/src/integrations/breadcrumbs.ts
Line 36 in 3d63621
sentry-javascript/packages/opentelemetry/src/utils/parseSpanDescription.ts
Line 23 in 3d63621
sentry-javascript/packages/node/src/integrations/http/SentryHttpInstrumentation.ts
Line 17 in 3d63621
sentry-javascript/packages/node/src/integrations/node-fetch/SentryNodeFetchInstrumentation.ts
Line 6 in 3d63621
sentry-javascript/packages/browser/src/tracing/request.ts
Line 22 in 3d63621
sentry-javascript/packages/browser-utils/src/metrics/browserMetrics.ts
Line 9 in 3d63621
With that, we can also refactor
getSanitizedUrlString, which should provide some performance benefits.sentry-javascript/packages/core/src/utils-hoist/url.ts
Line 55 in 3d63621
sentry-javascript/packages/bun/src/integrations/bunserver.ts
Line 10 in 3d63621
sentry-javascript/packages/opentelemetry/src/utils/getRequestSpanData.ts
Line 9 in 3d63621
sentry-javascript/packages/node/src/integrations/node-fetch/SentryNodeFetchInstrumentation.ts
Line 6 in 3d63621
sentry-javascript/packages/node/src/integrations/http/SentryHttpInstrumentation.ts
Line 14 in 3d63621
sentry-javascript/packages/opentelemetry/src/utils/parseSpanDescription.ts
Line 22 in 3d63621
Reminder list:
extractQueryParamsFromUrl?