|
| 1 | +import type { Client } from '@sentry/core'; |
| 2 | +import { debug, getClient, LRUMap, parseStringToURLObject, safeCallback } from '@sentry/core'; |
| 3 | +import { DEBUG_BUILD } from './debug-build'; |
| 4 | +import { getLocationHref } from './getLocationHref'; |
| 5 | + |
| 6 | +/** The parts of a URL a route depends on. A `URL` satisfies it. */ |
| 7 | +type RouteUrl = Pick<URL, 'pathname' | 'search' | 'hash'>; |
| 8 | + |
| 9 | +/** |
| 10 | + * Resolves URLs to low-cardinality route names. |
| 11 | + * |
| 12 | + * Framework SDKs register one so that everything the SDK names after a route (span names, the scope's |
| 13 | + * transaction name, metric and span segment attributes) gets the parameterized route instead of the raw |
| 14 | + * URL, without each integration having to reach into the framework's router itself. |
| 15 | + */ |
| 16 | +export interface RouteProvider { |
| 17 | + /** |
| 18 | + * Resolves a URL path template for a specific URL, e.g. `/users/42` -> `/users/:id`. |
| 19 | + * |
| 20 | + * Must return a path template, never a route identifier. Routers that name routes independently of |
| 21 | + * their path (Vue Router's `route.name`, Ember's `posts.show`) have to return the matched path |
| 22 | + * instead: callers set `url.template` from this, and an identifier is not a template. An SDK that |
| 23 | + * wants to name its span after the identifier still can, on the span itself. |
| 24 | + * |
| 25 | + * Returns `undefined` when the URL matches no known route. Must answer for the URL it is given rather |
| 26 | + * than for wherever the router currently is, so that callers can resolve a URL they captured earlier |
| 27 | + * (a web vital reported after a soft navigation, for example). |
| 28 | + */ |
| 29 | + resolveRoute(url: RouteUrl): string | undefined; |
| 30 | + |
| 31 | + /** |
| 32 | + * Resolves the route the app is currently on. |
| 33 | + * |
| 34 | + * Routers whose location lives in the address bar can delegate to `resolveRoute`, which is what |
| 35 | + * {@link createUrlRouteProvider} does. Routers that keep their own location (memory and hash routers) |
| 36 | + * have to answer from that location instead: for those, `location.href` is the unchanging shell URL |
| 37 | + * and would bucket every route together. |
| 38 | + */ |
| 39 | + resolveCurrentRoute(): string | undefined; |
| 40 | +} |
| 41 | + |
| 42 | +const CLIENT_ROUTE_PROVIDERS = new WeakMap<Client, RouteProvider>(); |
| 43 | + |
| 44 | +/** |
| 45 | + * Registers the route provider for a client, replacing any previously registered one. `BrowserClient` |
| 46 | + * calls this with its `routeProvider` option. |
| 47 | + * |
| 48 | + * Prefer the `routeProvider` option where the provider is known at `init`: the pageload span is named |
| 49 | + * while `browserTracingIntegration` sets up, so a provider registered after `init` can only rename it |
| 50 | + * after the fact. |
| 51 | + * |
| 52 | + * A client holds one provider. An app running two routers (a framework migration, or a shell plus an |
| 53 | + * island) registers twice and the last one wins, so the first router's routes stop resolving. |
| 54 | + */ |
| 55 | +export function setRouteProvider(provider: RouteProvider, client: Client | undefined = getClient()): void { |
| 56 | + if (!client) { |
| 57 | + DEBUG_BUILD && debug.warn('Cannot set a route provider without a client.'); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (DEBUG_BUILD && getRouteProvider(client)) { |
| 62 | + debug.warn( |
| 63 | + 'A route provider is already registered for this client and will be replaced. Routes only the previous provider knows about will no longer resolve.', |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + CLIENT_ROUTE_PROVIDERS.set(client, provider); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Returns the route provider registered for a client. |
| 72 | + */ |
| 73 | +export function getRouteProvider(client: Client | undefined = getClient()): RouteProvider | undefined { |
| 74 | + return client && CLIENT_ROUTE_PROVIDERS.get(client); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Resolves a URL to a low-cardinality route name, e.g. `/users/42` -> `/users/:id`. |
| 79 | + * |
| 80 | + * Returns `undefined` when no route provider is registered or the URL matches no route. Callers pick |
| 81 | + * their own fallback, because the right one differs: a span name falls back to a low-cardinality |
| 82 | + * constant, the scope's transaction name to the raw path. |
| 83 | + */ |
| 84 | +export function resolveRoute(url: string | URL, client: Client | undefined = getClient()): string | undefined { |
| 85 | + const provider = getRouteProvider(client); |
| 86 | + if (!provider) { |
| 87 | + return undefined; |
| 88 | + } |
| 89 | + |
| 90 | + const urlObject = typeof url === 'string' ? parseLocation(url) : url; |
| 91 | + if (!urlObject) { |
| 92 | + return undefined; |
| 93 | + } |
| 94 | + |
| 95 | + return safeCallback( |
| 96 | + DEBUG_BUILD ? 'Route provider threw while resolving a route:' : '', |
| 97 | + () => provider.resolveRoute(urlObject) || undefined, |
| 98 | + () => undefined, |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Resolves the route the app is currently on. |
| 104 | + * |
| 105 | + * Returns `undefined` when no route provider is registered or the current location matches no route. |
| 106 | + */ |
| 107 | +export function resolveCurrentRoute(client: Client | undefined = getClient()): string | undefined { |
| 108 | + const provider = getRouteProvider(client); |
| 109 | + |
| 110 | + return safeCallback( |
| 111 | + DEBUG_BUILD ? 'Route provider threw while resolving a route:' : '', |
| 112 | + () => provider?.resolveCurrentRoute() || undefined, |
| 113 | + () => undefined, |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Builds a {@link RouteProvider} for a router whose location is the browser's, which covers every |
| 119 | + * router except memory and hash routers. |
| 120 | + */ |
| 121 | +export function createUrlRouteProvider(resolveRouteFromUrl: (url: RouteUrl) => string | undefined): RouteProvider { |
| 122 | + return { |
| 123 | + resolveRoute: resolveRouteFromUrl, |
| 124 | + resolveCurrentRoute: () => { |
| 125 | + const urlObject = parseLocation(getLocationHref()); |
| 126 | + |
| 127 | + return urlObject && resolveRouteFromUrl(urlObject); |
| 128 | + }, |
| 129 | + }; |
| 130 | +} |
| 131 | + |
| 132 | +/** |
| 133 | + * A {@link RouteProvider} that answers from routes it has been told about, rather than by matching. |
| 134 | + */ |
| 135 | +export interface CachedRouteProvider extends RouteProvider { |
| 136 | + /** Records the route name a router reported for a path. Ignores empty values. */ |
| 137 | + record(pathname: string | undefined, routeName: string | null | undefined): void; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * Builds a route provider for a router with no usable matcher, which can only report the route it is |
| 142 | + * on as it gets there (SvelteKit's `page.route.id`, Solid Router's current matches). |
| 143 | + * |
| 144 | + * A URL the app has not visited resolves to `undefined`, which includes the first pageload until the |
| 145 | + * router reports. Backed by an LRU so a long-lived app visiting many URLs can't grow it without end, |
| 146 | + * and so routes that keep being resolved outlive ones passed through once. |
| 147 | + */ |
| 148 | +export function createCachedRouteProvider(maxEntries: number = 50): CachedRouteProvider { |
| 149 | + const routeNames = new LRUMap<string, string>(maxEntries); |
| 150 | + |
| 151 | + return { |
| 152 | + ...createUrlRouteProvider(url => routeNames.get(url.pathname)), |
| 153 | + record(pathname, routeName) { |
| 154 | + if (pathname && routeName) { |
| 155 | + routeNames.set(pathname, routeName); |
| 156 | + } |
| 157 | + }, |
| 158 | + }; |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Parses up front so providers never have to, and resolves relative locations (which memory routers |
| 163 | + * hand around) against the document. |
| 164 | + */ |
| 165 | +function parseLocation(url: string): RouteUrl | undefined { |
| 166 | + // Without a document `getLocationHref()` is empty, which would otherwise parse as `/`. |
| 167 | + return url ? parseStringToURLObject(url, getLocationHref() || undefined) : undefined; |
| 168 | +} |
0 commit comments