@depup/sentry__react 10.44.0-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/build/cjs/constants.js +12 -0
  4. package/build/cjs/constants.js.map +1 -0
  5. package/build/cjs/debug-build.js +11 -0
  6. package/build/cjs/debug-build.js.map +1 -0
  7. package/build/cjs/error.js +113 -0
  8. package/build/cjs/error.js.map +1 -0
  9. package/build/cjs/errorboundary.js +170 -0
  10. package/build/cjs/errorboundary.js.map +1 -0
  11. package/build/cjs/hoist-non-react-statics.js +159 -0
  12. package/build/cjs/hoist-non-react-statics.js.map +1 -0
  13. package/build/cjs/index.js +51 -0
  14. package/build/cjs/index.js.map +1 -0
  15. package/build/cjs/profiler.js +226 -0
  16. package/build/cjs/profiler.js.map +1 -0
  17. package/build/cjs/reactrouter-compat-utils/instrumentation.js +1295 -0
  18. package/build/cjs/reactrouter-compat-utils/instrumentation.js.map +1 -0
  19. package/build/cjs/reactrouter-compat-utils/lazy-routes.js +160 -0
  20. package/build/cjs/reactrouter-compat-utils/lazy-routes.js.map +1 -0
  21. package/build/cjs/reactrouter-compat-utils/route-manifest.js +194 -0
  22. package/build/cjs/reactrouter-compat-utils/route-manifest.js.map +1 -0
  23. package/build/cjs/reactrouter-compat-utils/utils.js +336 -0
  24. package/build/cjs/reactrouter-compat-utils/utils.js.map +1 -0
  25. package/build/cjs/reactrouter.js +236 -0
  26. package/build/cjs/reactrouter.js.map +1 -0
  27. package/build/cjs/reactrouterv3.js +130 -0
  28. package/build/cjs/reactrouterv3.js.map +1 -0
  29. package/build/cjs/reactrouterv6.js +61 -0
  30. package/build/cjs/reactrouterv6.js.map +1 -0
  31. package/build/cjs/reactrouterv7.js +61 -0
  32. package/build/cjs/reactrouterv7.js.map +1 -0
  33. package/build/cjs/redux.js +106 -0
  34. package/build/cjs/redux.js.map +1 -0
  35. package/build/cjs/sdk.js +21 -0
  36. package/build/cjs/sdk.js.map +1 -0
  37. package/build/cjs/tanstackrouter.js +134 -0
  38. package/build/cjs/tanstackrouter.js.map +1 -0
  39. package/build/esm/constants.js +8 -0
  40. package/build/esm/constants.js.map +1 -0
  41. package/build/esm/debug-build.js +9 -0
  42. package/build/esm/debug-build.js.map +1 -0
  43. package/build/esm/error.js +108 -0
  44. package/build/esm/error.js.map +1 -0
  45. package/build/esm/errorboundary.js +166 -0
  46. package/build/esm/errorboundary.js.map +1 -0
  47. package/build/esm/hoist-non-react-statics.js +157 -0
  48. package/build/esm/hoist-non-react-statics.js.map +1 -0
  49. package/build/esm/index.js +12 -0
  50. package/build/esm/index.js.map +1 -0
  51. package/build/esm/package.json +1 -0
  52. package/build/esm/profiler.js +221 -0
  53. package/build/esm/profiler.js.map +1 -0
  54. package/build/esm/reactrouter-compat-utils/instrumentation.js +1281 -0
  55. package/build/esm/reactrouter-compat-utils/instrumentation.js.map +1 -0
  56. package/build/esm/reactrouter-compat-utils/lazy-routes.js +156 -0
  57. package/build/esm/reactrouter-compat-utils/lazy-routes.js.map +1 -0
  58. package/build/esm/reactrouter-compat-utils/route-manifest.js +191 -0
  59. package/build/esm/reactrouter-compat-utils/route-manifest.js.map +1 -0
  60. package/build/esm/reactrouter-compat-utils/utils.js +320 -0
  61. package/build/esm/reactrouter-compat-utils/utils.js.map +1 -0
  62. package/build/esm/reactrouter.js +232 -0
  63. package/build/esm/reactrouter.js.map +1 -0
  64. package/build/esm/reactrouterv3.js +128 -0
  65. package/build/esm/reactrouterv3.js.map +1 -0
  66. package/build/esm/reactrouterv6.js +55 -0
  67. package/build/esm/reactrouterv6.js.map +1 -0
  68. package/build/esm/reactrouterv7.js +55 -0
  69. package/build/esm/reactrouterv7.js.map +1 -0
  70. package/build/esm/redux.js +104 -0
  71. package/build/esm/redux.js.map +1 -0
  72. package/build/esm/sdk.js +19 -0
  73. package/build/esm/sdk.js.map +1 -0
  74. package/build/esm/tanstackrouter.js +132 -0
  75. package/build/esm/tanstackrouter.js.map +1 -0
  76. package/build/types/constants.d.ts +4 -0
  77. package/build/types/constants.d.ts.map +1 -0
  78. package/build/types/debug-build.d.ts +7 -0
  79. package/build/types/debug-build.d.ts.map +1 -0
  80. package/build/types/error.d.ts +41 -0
  81. package/build/types/error.d.ts.map +1 -0
  82. package/build/types/errorboundary.d.ts +87 -0
  83. package/build/types/errorboundary.d.ts.map +1 -0
  84. package/build/types/hoist-non-react-statics.d.ts +21 -0
  85. package/build/types/hoist-non-react-statics.d.ts.map +1 -0
  86. package/build/types/index.d.ts +13 -0
  87. package/build/types/index.d.ts.map +1 -0
  88. package/build/types/profiler.d.ts +56 -0
  89. package/build/types/profiler.d.ts.map +1 -0
  90. package/build/types/reactrouter-compat-utils/index.d.ts +5 -0
  91. package/build/types/reactrouter-compat-utils/index.d.ts.map +1 -0
  92. package/build/types/reactrouter-compat-utils/instrumentation.d.ts +117 -0
  93. package/build/types/reactrouter-compat-utils/instrumentation.d.ts.map +1 -0
  94. package/build/types/reactrouter-compat-utils/lazy-routes.d.ts +18 -0
  95. package/build/types/reactrouter-compat-utils/lazy-routes.d.ts.map +1 -0
  96. package/build/types/reactrouter-compat-utils/route-manifest.d.ts +13 -0
  97. package/build/types/reactrouter-compat-utils/route-manifest.d.ts.map +1 -0
  98. package/build/types/reactrouter-compat-utils/utils.d.ts +71 -0
  99. package/build/types/reactrouter-compat-utils/utils.d.ts.map +1 -0
  100. package/build/types/reactrouter.d.ts +41 -0
  101. package/build/types/reactrouter.d.ts.map +1 -0
  102. package/build/types/reactrouterv3.d.ts +29 -0
  103. package/build/types/reactrouterv3.d.ts.map +1 -0
  104. package/build/types/reactrouterv6.d.ts +32 -0
  105. package/build/types/reactrouterv6.d.ts.map +1 -0
  106. package/build/types/reactrouterv7.d.ts +32 -0
  107. package/build/types/reactrouterv7.d.ts.map +1 -0
  108. package/build/types/redux.d.ts +38 -0
  109. package/build/types/redux.d.ts.map +1 -0
  110. package/build/types/sdk.d.ts +7 -0
  111. package/build/types/sdk.d.ts.map +1 -0
  112. package/build/types/tanstackrouter.d.ts +13 -0
  113. package/build/types/tanstackrouter.d.ts.map +1 -0
  114. package/build/types/types.d.ts +163 -0
  115. package/build/types/types.d.ts.map +1 -0
  116. package/build/types/vendor/tanstackrouter-types.d.ts +36 -0
  117. package/build/types/vendor/tanstackrouter-types.d.ts.map +1 -0
  118. package/build/types-ts3.8/constants.d.ts +4 -0
  119. package/build/types-ts3.8/debug-build.d.ts +7 -0
  120. package/build/types-ts3.8/error.d.ts +41 -0
  121. package/build/types-ts3.8/errorboundary.d.ts +87 -0
  122. package/build/types-ts3.8/hoist-non-react-statics.d.ts +21 -0
  123. package/build/types-ts3.8/index.d.ts +13 -0
  124. package/build/types-ts3.8/profiler.d.ts +56 -0
  125. package/build/types-ts3.8/reactrouter-compat-utils/index.d.ts +5 -0
  126. package/build/types-ts3.8/reactrouter-compat-utils/instrumentation.d.ts +117 -0
  127. package/build/types-ts3.8/reactrouter-compat-utils/lazy-routes.d.ts +18 -0
  128. package/build/types-ts3.8/reactrouter-compat-utils/route-manifest.d.ts +13 -0
  129. package/build/types-ts3.8/reactrouter-compat-utils/utils.d.ts +77 -0
  130. package/build/types-ts3.8/reactrouter.d.ts +41 -0
  131. package/build/types-ts3.8/reactrouterv3.d.ts +29 -0
  132. package/build/types-ts3.8/reactrouterv6.d.ts +32 -0
  133. package/build/types-ts3.8/reactrouterv7.d.ts +32 -0
  134. package/build/types-ts3.8/redux.d.ts +38 -0
  135. package/build/types-ts3.8/sdk.d.ts +7 -0
  136. package/build/types-ts3.8/tanstackrouter.d.ts +13 -0
  137. package/build/types-ts3.8/types.d.ts +163 -0
  138. package/build/types-ts3.8/vendor/tanstackrouter-types.d.ts +36 -0
  139. package/changes.json +5 -0
  140. package/package.json +108 -0
@@ -0,0 +1,1281 @@
1
+ import { browserTracingIntegration, WINDOW, startBrowserTracingPageLoadSpan, startBrowserTracingNavigationSpan } from '@sentry/browser';
2
+ import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, debug, addNonEnumerableProperty, spanToJSON, getCurrentScope, getClient } from '@sentry/core';
3
+ import * as React from 'react';
4
+ import { DEBUG_BUILD } from '../debug-build.js';
5
+ import { hoistNonReactStatics } from '../hoist-non-react-statics.js';
6
+ import { checkRouteForAsyncHandler } from './lazy-routes.js';
7
+ import { getActiveRootSpan, setNavigationContext, clearNavigationContext, resolveRouteNameAndSource, transactionNameHasWildcard, initializeRouterUtils } from './utils.js';
8
+
9
+ /* eslint-disable max-lines */
10
+ // Inspired from Donnie McNeal's solution:
11
+ // https://gist.github.com/wontondon/e8c4bdf2888875e4c755712e99279536
12
+
13
+
14
+ let _useEffect;
15
+ let _useLocation;
16
+ let _useNavigationType;
17
+ let _createRoutesFromChildren;
18
+ let _matchRoutes;
19
+
20
+ let _enableAsyncRouteHandlers = false;
21
+ let _lazyRouteTimeout = 3000;
22
+ let _lazyRouteManifest;
23
+ let _basename = '';
24
+
25
+ const CLIENTS_WITH_INSTRUMENT_NAVIGATION = new WeakSet();
26
+
27
+ // Prevents duplicate spans when router.subscribe fires multiple times
28
+ const activeNavigationSpans = new WeakMap
29
+
30
+ ();
31
+
32
+ // Exported for testing only
33
+ const allRoutes = new Set();
34
+
35
+ // Tracks lazy route loads to wait before finalizing span names
36
+ const pendingLazyRouteLoads = new WeakMap();
37
+
38
+ // Tracks deferred lazy route promises that can be resolved when patchRoutesOnNavigation is called
39
+ const deferredLazyRouteResolvers = new WeakMap();
40
+
41
+ /**
42
+ * Schedules a callback using requestAnimationFrame when available (browser),
43
+ * or falls back to setTimeout for SSR environments (Node.js, createMemoryRouter tests).
44
+ */
45
+ function scheduleCallback(callback) {
46
+ if (WINDOW?.requestAnimationFrame) {
47
+ return WINDOW.requestAnimationFrame(callback);
48
+ }
49
+ return setTimeout(callback, 0) ;
50
+ }
51
+
52
+ /**
53
+ * Cancels a scheduled callback, handling both RAF (browser) and timeout (SSR) IDs.
54
+ */
55
+ function cancelScheduledCallback(id) {
56
+ if (WINDOW?.cancelAnimationFrame) {
57
+ WINDOW.cancelAnimationFrame(id);
58
+ } else {
59
+ clearTimeout(id);
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Computes location key for duplicate detection. Normalizes undefined/null to empty strings.
65
+ * Exported for testing.
66
+ */
67
+ function computeLocationKey(location) {
68
+ return `${location.pathname}${location.search || ''}${location.hash || ''}`;
69
+ }
70
+
71
+ /**
72
+ * Checks if a route name is parameterized (contains route parameters like :id or wildcards like *)
73
+ * vs a raw URL path.
74
+ */
75
+ function isParameterizedRoute(routeName) {
76
+ return routeName.includes(':') || routeName.includes('*');
77
+ }
78
+
79
+ /**
80
+ * Determines if a navigation should be skipped as a duplicate, and if an existing span should be updated.
81
+ * Exported for testing.
82
+ *
83
+ * @returns An object with:
84
+ * - skip: boolean - Whether to skip creating a new span
85
+ * - shouldUpdate: boolean - Whether to update the existing span name (wildcard upgrade)
86
+ */
87
+ function shouldSkipNavigation(
88
+ trackedNav
89
+
90
+ ,
91
+ locationKey,
92
+ proposedName,
93
+ spanHasEnded,
94
+ ) {
95
+ if (!trackedNav) {
96
+ return { skip: false, shouldUpdate: false };
97
+ }
98
+
99
+ // Check if this is a duplicate navigation (same location)
100
+ // 1. If it's a placeholder, it's always a duplicate (we're waiting for the real one)
101
+ // 2. If it's a real span, it's a duplicate only if it hasn't ended yet
102
+ const isDuplicate = trackedNav.locationKey === locationKey && (trackedNav.isPlaceholder || !spanHasEnded);
103
+
104
+ if (isDuplicate) {
105
+ // Check if we should update the span name with a better route
106
+ // Allow updates if:
107
+ // 1. Current has wildcard and new doesn't (wildcard → parameterized upgrade)
108
+ // 2. Current is raw path and new is parameterized (raw → parameterized upgrade)
109
+ // 3. New name is different and more specific (longer, indicating nested routes resolved)
110
+ const currentHasWildcard = !!trackedNav.routeName && transactionNameHasWildcard(trackedNav.routeName);
111
+ const proposedHasWildcard = transactionNameHasWildcard(proposedName);
112
+ const currentIsParameterized = !!trackedNav.routeName && isParameterizedRoute(trackedNav.routeName);
113
+ const proposedIsParameterized = isParameterizedRoute(proposedName);
114
+
115
+ const isWildcardUpgrade = currentHasWildcard && !proposedHasWildcard;
116
+ const isRawToParameterized = !currentIsParameterized && proposedIsParameterized;
117
+ const isMoreSpecific =
118
+ proposedName !== trackedNav.routeName &&
119
+ proposedName.length > (trackedNav.routeName?.length || 0) &&
120
+ !proposedHasWildcard;
121
+
122
+ const shouldUpdate = !!(trackedNav.routeName && (isWildcardUpgrade || isRawToParameterized || isMoreSpecific));
123
+
124
+ return { skip: true, shouldUpdate };
125
+ }
126
+
127
+ return { skip: false, shouldUpdate: false };
128
+ }
129
+
130
+ function addResolvedRoutesToParent(resolvedRoutes, parentRoute) {
131
+ const existingChildren = parentRoute.children || [];
132
+
133
+ const newRoutes = resolvedRoutes.filter(
134
+ newRoute =>
135
+ !existingChildren.some(
136
+ existing =>
137
+ existing === newRoute ||
138
+ (newRoute.path && existing.path === newRoute.path) ||
139
+ (newRoute.id && existing.id === newRoute.id),
140
+ ),
141
+ );
142
+
143
+ if (newRoutes.length > 0) {
144
+ parentRoute.children = [...existingChildren, ...newRoutes];
145
+ }
146
+ }
147
+
148
+ /** Registers a pending lazy route load promise for a span. */
149
+ function trackLazyRouteLoad(span, promise) {
150
+ let promises = pendingLazyRouteLoads.get(span);
151
+ if (!promises) {
152
+ promises = new Set();
153
+ pendingLazyRouteLoads.set(span, promises);
154
+ }
155
+ promises.add(promise);
156
+
157
+ // Clean up when promise resolves/rejects
158
+ // oxlint-disable-next-line typescript/no-floating-promises
159
+ promise.finally(() => {
160
+ const currentPromises = pendingLazyRouteLoads.get(span);
161
+ if (currentPromises) {
162
+ currentPromises.delete(promise);
163
+ }
164
+ });
165
+ }
166
+
167
+ /**
168
+ * Creates a deferred promise for a span that will be resolved when patchRoutesOnNavigation is called.
169
+ * This ensures that patchedEnd waits for patchRoutesOnNavigation to be called before ending the span.
170
+ */
171
+ function createDeferredLazyRoutePromise(span) {
172
+ const deferredPromise = new Promise(resolve => {
173
+ deferredLazyRouteResolvers.set(span, resolve);
174
+ });
175
+
176
+ trackLazyRouteLoad(span, deferredPromise);
177
+ }
178
+
179
+ /**
180
+ * Resolves the deferred lazy route promise for a span.
181
+ * Called when patchRoutesOnNavigation is invoked.
182
+ */
183
+ function resolveDeferredLazyRoutePromise(span) {
184
+ const resolver = deferredLazyRouteResolvers.get(span);
185
+ if (resolver) {
186
+ resolver();
187
+ deferredLazyRouteResolvers.delete(span);
188
+ // Clear the flag so patchSpanEnd doesn't wait unnecessarily for routes that have already loaded
189
+ if ((span ).__sentry_may_have_lazy_routes__) {
190
+ (span ).__sentry_may_have_lazy_routes__ = false;
191
+ }
192
+ }
193
+ }
194
+
195
+ /**
196
+ * Processes resolved routes by adding them to allRoutes and checking for nested async handlers.
197
+ * When capturedSpan is provided, updates that specific span instead of the current active span.
198
+ * This prevents race conditions where a lazy handler resolves after the user has navigated away.
199
+ */
200
+ function processResolvedRoutes(
201
+ resolvedRoutes,
202
+ parentRoute,
203
+ currentLocation = null,
204
+ capturedSpan,
205
+ ) {
206
+ resolvedRoutes.forEach(child => {
207
+ allRoutes.add(child);
208
+ // Only check for async handlers if the feature is enabled
209
+ if (_enableAsyncRouteHandlers) {
210
+ checkRouteForAsyncHandler(child, processResolvedRoutes);
211
+ }
212
+ });
213
+
214
+ if (parentRoute) {
215
+ // If a parent route is provided, add the resolved routes as children to the parent route
216
+ addResolvedRoutesToParent(resolvedRoutes, parentRoute);
217
+ }
218
+
219
+ // Use captured span if provided, otherwise fall back to current active span
220
+ const targetSpan = capturedSpan ?? getActiveRootSpan();
221
+ if (targetSpan) {
222
+ const spanJson = spanToJSON(targetSpan);
223
+
224
+ // Skip update if span has already ended (timestamp is set when span.end() is called)
225
+ if (spanJson.timestamp) {
226
+ DEBUG_BUILD && debug.warn('[React Router] Lazy handler resolved after span ended - skipping update');
227
+ return;
228
+ }
229
+
230
+ const spanOp = spanJson.op;
231
+
232
+ // Use captured location for route matching (ensures we match against the correct route)
233
+ // Fall back to window.location only if no captured location and no captured span
234
+ // (i.e., this is not from an async handler)
235
+ let location = currentLocation;
236
+ if (!location && !capturedSpan) {
237
+ if (typeof WINDOW !== 'undefined') {
238
+ const globalLocation = WINDOW.location;
239
+ if (globalLocation?.pathname) {
240
+ location = { pathname: globalLocation.pathname };
241
+ }
242
+ }
243
+ }
244
+
245
+ if (location) {
246
+ if (spanOp === 'pageload') {
247
+ // Re-run the pageload transaction update with the newly loaded routes
248
+ updatePageloadTransaction({
249
+ activeRootSpan: targetSpan,
250
+ location: { pathname: location.pathname },
251
+ routes: Array.from(allRoutes),
252
+ allRoutes: Array.from(allRoutes),
253
+ });
254
+ } else if (spanOp === 'navigation') {
255
+ // For navigation spans, update the name with the newly loaded routes
256
+ updateNavigationSpan(targetSpan, location, Array.from(allRoutes), false, _matchRoutes);
257
+ }
258
+ }
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Updates a navigation span with the correct route name after lazy routes have been loaded.
264
+ */
265
+ function updateNavigationSpan(
266
+ activeRootSpan,
267
+ location,
268
+ allRoutes,
269
+ forceUpdate = false,
270
+ matchRoutes,
271
+ ) {
272
+ const spanJson = spanToJSON(activeRootSpan);
273
+ const currentName = spanJson.description;
274
+
275
+ const hasBeenNamed = (activeRootSpan )?.__sentry_navigation_name_set__;
276
+ const currentNameHasWildcard = currentName && transactionNameHasWildcard(currentName);
277
+ const shouldUpdate = !hasBeenNamed || forceUpdate || currentNameHasWildcard;
278
+
279
+ if (shouldUpdate && !spanJson.timestamp) {
280
+ const currentBranches = matchRoutes(allRoutes, location);
281
+ const [name, source] = resolveRouteNameAndSource(
282
+ location,
283
+ allRoutes,
284
+ allRoutes,
285
+ (currentBranches ) || [],
286
+ _basename,
287
+ _lazyRouteManifest,
288
+ _enableAsyncRouteHandlers,
289
+ );
290
+
291
+ const currentSource = spanJson.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
292
+ const isImprovement =
293
+ name &&
294
+ (!currentName || // No current name - always set
295
+ (!hasBeenNamed && (currentSource !== 'route' || source === 'route')) || // Not finalized - allow unless downgrading route→url
296
+ (currentSource !== 'route' && source === 'route') || // URL → route upgrade
297
+ (currentSource === 'route' && source === 'route' && currentNameHasWildcard)); // Route → better route (only if current has wildcard)
298
+ if (isImprovement) {
299
+ activeRootSpan.updateName(name);
300
+ activeRootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
301
+
302
+ // Only mark as finalized for non-wildcard route names (allows URL→route upgrades).
303
+ if (!transactionNameHasWildcard(name) && source === 'route') {
304
+ addNonEnumerableProperty(
305
+ activeRootSpan ,
306
+ '__sentry_navigation_name_set__',
307
+ true,
308
+ );
309
+ }
310
+ }
311
+ }
312
+ }
313
+
314
+ function setupRouterSubscription(
315
+ router,
316
+ routes,
317
+ version,
318
+ basename,
319
+ activeRootSpan,
320
+ ) {
321
+ let isInitialPageloadComplete = false;
322
+ let hasSeenPageloadSpan = !!activeRootSpan && spanToJSON(activeRootSpan).op === 'pageload';
323
+ let hasSeenPopAfterPageload = false;
324
+ let scheduledNavigationHandler = null;
325
+ let lastHandledPathname = null;
326
+
327
+ router.subscribe((state) => {
328
+ if (!isInitialPageloadComplete) {
329
+ const currentRootSpan = getActiveRootSpan();
330
+ const isCurrentlyInPageload = currentRootSpan && spanToJSON(currentRootSpan).op === 'pageload';
331
+
332
+ if (isCurrentlyInPageload) {
333
+ hasSeenPageloadSpan = true;
334
+ } else if (hasSeenPageloadSpan) {
335
+ if (state.historyAction === 'POP' && !hasSeenPopAfterPageload) {
336
+ hasSeenPopAfterPageload = true;
337
+ } else {
338
+ isInitialPageloadComplete = true;
339
+ }
340
+ }
341
+ }
342
+
343
+ const shouldHandleNavigation =
344
+ state.historyAction === 'PUSH' || (state.historyAction === 'POP' && isInitialPageloadComplete);
345
+
346
+ if (shouldHandleNavigation) {
347
+ // Include search and hash to allow query/hash-only navigations
348
+ // Use computeLocationKey() to ensure undefined/null values are normalized to empty strings
349
+ const currentLocationKey = computeLocationKey(state.location);
350
+ const navigationHandler = () => {
351
+ // Prevent multiple calls for the same location within the same navigation cycle
352
+ if (lastHandledPathname === currentLocationKey) {
353
+ return;
354
+ }
355
+ lastHandledPathname = currentLocationKey;
356
+ scheduledNavigationHandler = null;
357
+ handleNavigation({
358
+ location: state.location,
359
+ routes,
360
+ navigationType: state.historyAction,
361
+ version,
362
+ basename,
363
+ allRoutes: Array.from(allRoutes),
364
+ });
365
+ };
366
+
367
+ if (state.navigation.state !== 'idle') {
368
+ // Navigation in progress - reset if location changed
369
+ if (lastHandledPathname !== currentLocationKey) {
370
+ lastHandledPathname = null;
371
+ }
372
+ // Cancel any previously scheduled handler to avoid duplicates
373
+ if (scheduledNavigationHandler !== null) {
374
+ cancelScheduledCallback(scheduledNavigationHandler);
375
+ }
376
+ scheduledNavigationHandler = scheduleCallback(navigationHandler);
377
+ } else {
378
+ // Navigation completed - cancel scheduled handler if any, then call immediately
379
+ if (scheduledNavigationHandler !== null) {
380
+ cancelScheduledCallback(scheduledNavigationHandler);
381
+ scheduledNavigationHandler = null;
382
+ }
383
+ navigationHandler();
384
+ // Don't reset - next navigation cycle resets to prevent duplicates within same cycle.
385
+ }
386
+ }
387
+ });
388
+ }
389
+
390
+ /**
391
+ * Creates a wrapCreateBrowserRouter function that can be used with all React Router v6 compatible versions.
392
+ */
393
+ function createV6CompatibleWrapCreateBrowserRouter
394
+
395
+ (
396
+ createRouterFunction,
397
+ version,
398
+ ) {
399
+ if (!_useEffect || !_useLocation || !_useNavigationType || !_matchRoutes) {
400
+ DEBUG_BUILD &&
401
+ debug.warn(
402
+ `reactRouterV${version}Instrumentation was unable to wrap the \`createRouter\` function because of one or more missing parameters.`,
403
+ );
404
+
405
+ return createRouterFunction;
406
+ }
407
+
408
+ return function (routes, opts) {
409
+ addRoutesToAllRoutes(routes);
410
+
411
+ if (_enableAsyncRouteHandlers) {
412
+ for (const route of routes) {
413
+ checkRouteForAsyncHandler(route, processResolvedRoutes);
414
+ }
415
+ }
416
+
417
+ // Capture the active span BEFORE creating the router.
418
+ // This is important because the span might end (due to idle timeout) before
419
+ // patchRoutesOnNavigation is called by React Router.
420
+ const activeRootSpan = getActiveRootSpan();
421
+
422
+ // If patchRoutesOnNavigation is provided and we have an active span,
423
+ // mark the span as having potential lazy routes and create a deferred promise.
424
+ const hasPatchRoutesOnNavigation =
425
+ opts && 'patchRoutesOnNavigation' in opts && typeof opts.patchRoutesOnNavigation === 'function';
426
+ if (hasPatchRoutesOnNavigation && activeRootSpan) {
427
+ // Mark the span as potentially having lazy routes
428
+ addNonEnumerableProperty(
429
+ activeRootSpan ,
430
+ '__sentry_may_have_lazy_routes__',
431
+ true,
432
+ );
433
+ createDeferredLazyRoutePromise(activeRootSpan);
434
+ }
435
+
436
+ // Pass the captured span to wrapPatchRoutesOnNavigation so it uses the same span
437
+ // even if the span has ended by the time patchRoutesOnNavigation is called.
438
+ const wrappedOpts = wrapPatchRoutesOnNavigation(opts, false, activeRootSpan);
439
+ const router = createRouterFunction(routes, wrappedOpts);
440
+ const basename = opts?.basename;
441
+
442
+ if (router.state.historyAction === 'POP' && activeRootSpan) {
443
+ updatePageloadTransaction({
444
+ activeRootSpan,
445
+ location: router.state.location,
446
+ routes,
447
+ basename,
448
+ allRoutes: Array.from(allRoutes),
449
+ });
450
+ }
451
+
452
+ // Store basename for use in updateNavigationSpan
453
+ _basename = basename || '';
454
+
455
+ setupRouterSubscription(router, routes, version, basename, activeRootSpan);
456
+
457
+ return router;
458
+ };
459
+ }
460
+
461
+ /**
462
+ * Creates a wrapCreateMemoryRouter function that can be used with all React Router v6 compatible versions.
463
+ */
464
+ function createV6CompatibleWrapCreateMemoryRouter
465
+
466
+ (
467
+ createRouterFunction,
468
+ version,
469
+ ) {
470
+ if (!_useEffect || !_useLocation || !_useNavigationType || !_matchRoutes) {
471
+ DEBUG_BUILD &&
472
+ debug.warn(
473
+ `reactRouterV${version}Instrumentation was unable to wrap the \`createMemoryRouter\` function because of one or more missing parameters.`,
474
+ );
475
+
476
+ return createRouterFunction;
477
+ }
478
+
479
+ return function (
480
+ routes,
481
+ opts
482
+
483
+ ,
484
+ ) {
485
+ addRoutesToAllRoutes(routes);
486
+
487
+ if (_enableAsyncRouteHandlers) {
488
+ for (const route of routes) {
489
+ checkRouteForAsyncHandler(route, processResolvedRoutes);
490
+ }
491
+ }
492
+
493
+ // Capture the active span BEFORE creating the router (same as browser router)
494
+ const memoryActiveRootSpanEarly = getActiveRootSpan();
495
+
496
+ // If patchRoutesOnNavigation is provided and we have an active span,
497
+ // mark the span as having potential lazy routes and create a deferred promise.
498
+ const hasPatchRoutesOnNavigation =
499
+ opts && 'patchRoutesOnNavigation' in opts && typeof opts.patchRoutesOnNavigation === 'function';
500
+ if (hasPatchRoutesOnNavigation && memoryActiveRootSpanEarly) {
501
+ addNonEnumerableProperty(
502
+ memoryActiveRootSpanEarly ,
503
+ '__sentry_may_have_lazy_routes__',
504
+ true,
505
+ );
506
+ createDeferredLazyRoutePromise(memoryActiveRootSpanEarly);
507
+ }
508
+
509
+ const wrappedOpts = wrapPatchRoutesOnNavigation(opts, true, memoryActiveRootSpanEarly);
510
+
511
+ const router = createRouterFunction(routes, wrappedOpts);
512
+ const basename = opts?.basename;
513
+
514
+ let initialEntry = undefined;
515
+
516
+ const initialEntries = opts?.initialEntries;
517
+ const initialIndex = opts?.initialIndex;
518
+
519
+ const hasOnlyOneInitialEntry = initialEntries?.length === 1;
520
+ const hasIndexedEntry = initialIndex !== undefined && initialEntries?.[initialIndex];
521
+
522
+ initialEntry = hasOnlyOneInitialEntry
523
+ ? initialEntries[0]
524
+ : hasIndexedEntry
525
+ ? initialEntries[initialIndex]
526
+ : undefined;
527
+
528
+ const location = initialEntry
529
+ ? typeof initialEntry === 'string'
530
+ ? { pathname: initialEntry }
531
+ : initialEntry
532
+ : router.state.location;
533
+
534
+ const memoryActiveRootSpan = getActiveRootSpan();
535
+
536
+ if (router.state.historyAction === 'POP' && memoryActiveRootSpan) {
537
+ updatePageloadTransaction({
538
+ activeRootSpan: memoryActiveRootSpan,
539
+ location,
540
+ routes,
541
+ basename,
542
+ allRoutes: Array.from(allRoutes),
543
+ });
544
+ }
545
+
546
+ // Store basename for use in updateNavigationSpan
547
+ _basename = basename || '';
548
+
549
+ setupRouterSubscription(router, routes, version, basename, memoryActiveRootSpan);
550
+
551
+ return router;
552
+ };
553
+ }
554
+
555
+ /**
556
+ * Creates a browser tracing integration that can be used with all React Router v6 compatible versions.
557
+ */
558
+ function createReactRouterV6CompatibleTracingIntegration(
559
+ options,
560
+ version,
561
+ ) {
562
+ const integration = browserTracingIntegration({ ...options, instrumentPageLoad: false, instrumentNavigation: false });
563
+
564
+ const {
565
+ useEffect,
566
+ useLocation,
567
+ useNavigationType,
568
+ createRoutesFromChildren,
569
+ matchRoutes,
570
+ stripBasename,
571
+ enableAsyncRouteHandlers = false,
572
+ instrumentPageLoad = true,
573
+ instrumentNavigation = true,
574
+ lazyRouteTimeout,
575
+ lazyRouteManifest,
576
+ } = options;
577
+
578
+ return {
579
+ ...integration,
580
+ setup(client) {
581
+ integration.setup(client);
582
+
583
+ const finalTimeout = options.finalTimeout ?? 30000;
584
+ const defaultMaxWait = (options.idleTimeout ?? 1000) * 3;
585
+ const configuredMaxWait = lazyRouteTimeout ?? defaultMaxWait;
586
+
587
+ // Cap Infinity at finalTimeout to prevent indefinite hangs
588
+ if (configuredMaxWait === Infinity) {
589
+ _lazyRouteTimeout = finalTimeout;
590
+ DEBUG_BUILD &&
591
+ debug.log(
592
+ '[React Router] lazyRouteTimeout set to Infinity, capping at finalTimeout:',
593
+ finalTimeout,
594
+ 'ms to prevent indefinite hangs',
595
+ );
596
+ } else if (Number.isNaN(configuredMaxWait)) {
597
+ DEBUG_BUILD &&
598
+ debug.warn('[React Router] lazyRouteTimeout must be a number, falling back to default:', defaultMaxWait);
599
+ _lazyRouteTimeout = defaultMaxWait;
600
+ } else if (configuredMaxWait < 0) {
601
+ DEBUG_BUILD &&
602
+ debug.warn(
603
+ '[React Router] lazyRouteTimeout must be non-negative or Infinity, got:',
604
+ configuredMaxWait,
605
+ 'falling back to:',
606
+ defaultMaxWait,
607
+ );
608
+ _lazyRouteTimeout = defaultMaxWait;
609
+ } else {
610
+ _lazyRouteTimeout = configuredMaxWait;
611
+ }
612
+
613
+ _useEffect = useEffect;
614
+ _useLocation = useLocation;
615
+ _useNavigationType = useNavigationType;
616
+ _matchRoutes = matchRoutes;
617
+ _createRoutesFromChildren = createRoutesFromChildren;
618
+ _enableAsyncRouteHandlers = enableAsyncRouteHandlers;
619
+ _lazyRouteManifest = lazyRouteManifest;
620
+
621
+ // Initialize the router utils with the required dependencies
622
+ initializeRouterUtils(matchRoutes, stripBasename || false);
623
+ },
624
+ afterAllSetup(client) {
625
+ integration.afterAllSetup(client);
626
+
627
+ const initPathName = WINDOW.location?.pathname;
628
+ if (instrumentPageLoad && initPathName) {
629
+ startBrowserTracingPageLoadSpan(client, {
630
+ name: initPathName,
631
+ attributes: {
632
+ [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'url',
633
+ [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'pageload',
634
+ [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.pageload.react.reactrouter_v${version}`,
635
+ },
636
+ });
637
+ }
638
+
639
+ if (instrumentNavigation) {
640
+ CLIENTS_WITH_INSTRUMENT_NAVIGATION.add(client);
641
+ }
642
+ },
643
+ };
644
+ }
645
+
646
+ function createV6CompatibleWrapUseRoutes(origUseRoutes, version) {
647
+ if (!_useEffect || !_useLocation || !_useNavigationType || !_matchRoutes) {
648
+ DEBUG_BUILD &&
649
+ debug.warn(
650
+ 'reactRouterV6Instrumentation was unable to wrap `useRoutes` because of one or more missing parameters.',
651
+ );
652
+
653
+ return origUseRoutes;
654
+ }
655
+
656
+ const SentryRoutes
657
+
658
+ = (props) => {
659
+ const isMountRenderPass = React.useRef(true);
660
+ const { routes, locationArg } = props;
661
+
662
+ const Routes = origUseRoutes(routes, locationArg);
663
+
664
+ const location = _useLocation();
665
+ const navigationType = _useNavigationType();
666
+
667
+ // A value with stable identity to either pick `locationArg` if available or `location` if not
668
+ const stableLocationParam =
669
+ typeof locationArg === 'string' || locationArg?.pathname ? (locationArg ) : location;
670
+
671
+ _useEffect(() => {
672
+ const normalizedLocation =
673
+ typeof stableLocationParam === 'string' ? { pathname: stableLocationParam } : stableLocationParam;
674
+
675
+ if (isMountRenderPass.current) {
676
+ addRoutesToAllRoutes(routes);
677
+
678
+ updatePageloadTransaction({
679
+ activeRootSpan: getActiveRootSpan(),
680
+ location: normalizedLocation,
681
+ routes,
682
+ allRoutes: Array.from(allRoutes),
683
+ });
684
+ isMountRenderPass.current = false;
685
+ } else {
686
+ // Note: Component-based routes don't support lazy route tracking via lazyRouteTimeout
687
+ // because React.lazy() loads happen at the component level, not the router level.
688
+ // Use createBrowserRouter with patchRoutesOnNavigation for lazy route tracking.
689
+ handleNavigation({
690
+ location: normalizedLocation,
691
+ routes,
692
+ navigationType,
693
+ version,
694
+ allRoutes: Array.from(allRoutes),
695
+ });
696
+ }
697
+ }, [navigationType, stableLocationParam]);
698
+
699
+ return Routes;
700
+ };
701
+
702
+ // eslint-disable-next-line react/display-name
703
+ return (routes, locationArg) => {
704
+ return React.createElement(SentryRoutes, { routes: routes, locationArg: locationArg,} );
705
+ };
706
+ }
707
+ function wrapPatchRoutesOnNavigation(
708
+ opts,
709
+ isMemoryRouter = false,
710
+ capturedSpan,
711
+ ) {
712
+ if (!opts || !('patchRoutesOnNavigation' in opts) || typeof opts.patchRoutesOnNavigation !== 'function') {
713
+ return opts || {};
714
+ }
715
+
716
+ const originalPatchRoutes = opts.patchRoutesOnNavigation;
717
+ return {
718
+ ...opts,
719
+ patchRoutesOnNavigation: async (args) => {
720
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
721
+ const targetPath = (args )?.path;
722
+
723
+ // Use current active span if available, otherwise fall back to captured span (from router creation time).
724
+ // This ensures navigation spans use their own span (not the stale pageload span), while still
725
+ // supporting pageload spans that may have ended before patchRoutesOnNavigation is called.
726
+ const activeRootSpan = getActiveRootSpan() ?? capturedSpan;
727
+
728
+ if (!isMemoryRouter) {
729
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
730
+ const originalPatch = (args )?.patch;
731
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
732
+ const matches = (args )?.matches ;
733
+ if (originalPatch) {
734
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
735
+ (args ).patch = (routeId, children) => {
736
+ addRoutesToAllRoutes(children);
737
+
738
+ // Find the parent route from matches and attach children to it in allRoutes.
739
+ // React Router's patch attaches children to its internal route copies, but we need
740
+ // to update the route objects in our allRoutes Set for proper route matching.
741
+ if (matches && matches.length > 0) {
742
+ const leafMatch = matches[matches.length - 1];
743
+ const leafRoute = leafMatch?.route;
744
+ if (leafRoute) {
745
+ // Find the matching route in allRoutes by id, reference, or path
746
+ const matchingRoute = Array.from(allRoutes).find(route => {
747
+ const idMatches = route.id !== undefined && route.id === routeId;
748
+ const referenceMatches = route === leafRoute;
749
+ const pathMatches =
750
+ route.path !== undefined && leafRoute.path !== undefined && route.path === leafRoute.path;
751
+
752
+ return idMatches || referenceMatches || pathMatches;
753
+ });
754
+
755
+ if (matchingRoute) {
756
+ addResolvedRoutesToParent(children, matchingRoute);
757
+ }
758
+ }
759
+ }
760
+
761
+ // Use the captured activeRootSpan instead of getActiveRootSpan() to avoid race conditions
762
+ // where user navigates away during lazy route loading and we'd update the wrong span
763
+ const spanJson = activeRootSpan ? spanToJSON(activeRootSpan) : undefined;
764
+ // Only update if we have a valid targetPath (patchRoutesOnNavigation can be called without path),
765
+ // the captured span exists, hasn't ended, and is a navigation span
766
+ if (
767
+ targetPath &&
768
+ activeRootSpan &&
769
+ spanJson &&
770
+ !spanJson.timestamp && // Span hasn't ended yet
771
+ spanJson.op === 'navigation'
772
+ ) {
773
+ updateNavigationSpan(
774
+ activeRootSpan,
775
+ { pathname: targetPath, search: '', hash: '', state: null, key: 'default' },
776
+ Array.from(allRoutes),
777
+ true,
778
+ _matchRoutes,
779
+ );
780
+ }
781
+ return originalPatch(routeId, children);
782
+ };
783
+ }
784
+ }
785
+
786
+ const lazyLoadPromise = (async () => {
787
+ // Set context so async handlers can access correct targetPath and span
788
+ const contextToken = setNavigationContext(targetPath, activeRootSpan);
789
+ let result;
790
+ try {
791
+ result = await originalPatchRoutes(args);
792
+ } finally {
793
+ clearNavigationContext(contextToken);
794
+ // Resolve the deferred promise now that patchRoutesOnNavigation has completed.
795
+ // This ensures patchedEnd has waited long enough for the lazy routes to load.
796
+ if (activeRootSpan) {
797
+ resolveDeferredLazyRoutePromise(activeRootSpan);
798
+ }
799
+ }
800
+
801
+ // Use the captured activeRootSpan instead of getActiveRootSpan() to avoid race conditions
802
+ // where user navigates away during lazy route loading and we'd update the wrong span
803
+ const spanJson = activeRootSpan ? spanToJSON(activeRootSpan) : undefined;
804
+ if (
805
+ activeRootSpan &&
806
+ spanJson &&
807
+ !spanJson.timestamp && // Span hasn't ended yet
808
+ spanJson.op === 'navigation'
809
+ ) {
810
+ // Use targetPath consistently - don't fall back to WINDOW.location which may have changed
811
+ // if the user navigated away during async loading
812
+ const pathname = targetPath;
813
+
814
+ if (pathname) {
815
+ updateNavigationSpan(
816
+ activeRootSpan,
817
+ { pathname, search: '', hash: '', state: null, key: 'default' },
818
+ Array.from(allRoutes),
819
+ false,
820
+ _matchRoutes,
821
+ );
822
+ }
823
+ }
824
+
825
+ return result;
826
+ })();
827
+
828
+ if (activeRootSpan) {
829
+ trackLazyRouteLoad(activeRootSpan, lazyLoadPromise);
830
+ }
831
+
832
+ return lazyLoadPromise;
833
+ },
834
+ };
835
+ }
836
+
837
+ // eslint-disable-next-line complexity
838
+ function handleNavigation(opts
839
+
840
+ ) {
841
+ const { location, routes, navigationType, version, matches, basename, allRoutes } = opts;
842
+ const branches = Array.isArray(matches) ? matches : _matchRoutes(allRoutes || routes, location, basename);
843
+
844
+ const client = getClient();
845
+ if (!client || !CLIENTS_WITH_INSTRUMENT_NAVIGATION.has(client)) {
846
+ return;
847
+ }
848
+
849
+ const activeRootSpan = getActiveRootSpan();
850
+ if (activeRootSpan && spanToJSON(activeRootSpan).op === 'pageload' && navigationType === 'POP') {
851
+ return;
852
+ }
853
+
854
+ if ((navigationType === 'PUSH' || navigationType === 'POP') && branches) {
855
+ const [name, source] = resolveRouteNameAndSource(
856
+ location,
857
+ allRoutes || routes,
858
+ allRoutes || routes,
859
+ branches ,
860
+ basename,
861
+ _lazyRouteManifest,
862
+ _enableAsyncRouteHandlers,
863
+ );
864
+
865
+ const locationKey = computeLocationKey(location);
866
+ const trackedNav = activeNavigationSpans.get(client);
867
+
868
+ // Determine if this navigation should be skipped as a duplicate
869
+ const trackedSpanHasEnded =
870
+ trackedNav && !trackedNav.isPlaceholder ? !!spanToJSON(trackedNav.span).timestamp : false;
871
+ const { skip, shouldUpdate } = shouldSkipNavigation(trackedNav, locationKey, name, trackedSpanHasEnded);
872
+
873
+ if (skip) {
874
+ if (shouldUpdate && trackedNav) {
875
+ const oldName = trackedNav.routeName;
876
+
877
+ if (trackedNav.isPlaceholder) {
878
+ // Update placeholder's route name - the real span will be created with this name
879
+ trackedNav.routeName = name;
880
+ DEBUG_BUILD &&
881
+ debug.log(
882
+ `[Tracing] Updated placeholder navigation name from "${oldName}" to "${name}" (will apply to real span)`,
883
+ );
884
+ } else {
885
+ // Update existing real span from wildcard to parameterized route name
886
+ trackedNav.span.updateName(name);
887
+ trackedNav.span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source );
888
+ addNonEnumerableProperty(
889
+ trackedNav.span ,
890
+ '__sentry_navigation_name_set__',
891
+ true,
892
+ );
893
+ trackedNav.routeName = name;
894
+ DEBUG_BUILD && debug.log(`[Tracing] Updated navigation span name from "${oldName}" to "${name}"`);
895
+ }
896
+ } else {
897
+ DEBUG_BUILD && debug.log(`[Tracing] Skipping duplicate navigation for location: ${locationKey}`);
898
+ }
899
+ return;
900
+ }
901
+
902
+ // Create new navigation span (first navigation or legitimate new navigation)
903
+ // Reserve the spot in the map first to prevent race conditions
904
+ // Mark as placeholder to prevent concurrent handleNavigation calls from creating duplicates
905
+ const placeholderSpan = { end: () => {} } ;
906
+ const placeholderEntry = {
907
+ span: placeholderSpan,
908
+ routeName: name,
909
+ pathname: location.pathname,
910
+ locationKey,
911
+ isPlaceholder: true ,
912
+ };
913
+ activeNavigationSpans.set(client, placeholderEntry);
914
+
915
+ let navigationSpan;
916
+ try {
917
+ navigationSpan = startBrowserTracingNavigationSpan(client, {
918
+ name: placeholderEntry.routeName, // Use placeholder's routeName in case it was updated
919
+ attributes: {
920
+ [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: source,
921
+ [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
922
+ [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: `auto.navigation.react.reactrouter_v${version}`,
923
+ },
924
+ });
925
+ } catch (e) {
926
+ // If span creation fails, remove the placeholder so we don't block future navigations
927
+ activeNavigationSpans.delete(client);
928
+ throw e;
929
+ }
930
+
931
+ if (navigationSpan) {
932
+ // Update the map with the real span (isPlaceholder omitted, defaults to false)
933
+ activeNavigationSpans.set(client, {
934
+ span: navigationSpan,
935
+ routeName: placeholderEntry.routeName, // Use the (potentially updated) placeholder routeName
936
+ pathname: location.pathname,
937
+ locationKey,
938
+ });
939
+ patchSpanEnd(navigationSpan, location, routes, basename, 'navigation');
940
+ } else {
941
+ // If no span was created, remove the placeholder
942
+ activeNavigationSpans.delete(client);
943
+ }
944
+ }
945
+ }
946
+
947
+ /* Only exported for testing purposes */
948
+ function addRoutesToAllRoutes(routes) {
949
+ routes.forEach(route => {
950
+ const extractedChildRoutes = getChildRoutesRecursively(route);
951
+
952
+ extractedChildRoutes.forEach(r => {
953
+ allRoutes.add(r);
954
+ });
955
+ });
956
+ }
957
+
958
+ function getChildRoutesRecursively(route, allRoutes = new Set()) {
959
+ if (!allRoutes.has(route)) {
960
+ allRoutes.add(route);
961
+
962
+ if (route.children && !route.index) {
963
+ route.children.forEach(child => {
964
+ const childRoutes = getChildRoutesRecursively(child, allRoutes);
965
+
966
+ childRoutes.forEach(r => {
967
+ allRoutes.add(r);
968
+ });
969
+ });
970
+ }
971
+ }
972
+
973
+ return allRoutes;
974
+ }
975
+
976
+ function updatePageloadTransaction({
977
+ activeRootSpan,
978
+ location,
979
+ routes,
980
+ matches,
981
+ basename,
982
+ allRoutes,
983
+ }
984
+
985
+ ) {
986
+ const branches = Array.isArray(matches)
987
+ ? matches
988
+ : (_matchRoutes(allRoutes || routes, location, basename) );
989
+
990
+ if (branches) {
991
+ const [name, source] = resolveRouteNameAndSource(
992
+ location,
993
+ allRoutes || routes,
994
+ allRoutes || routes,
995
+ branches,
996
+ basename,
997
+ _lazyRouteManifest,
998
+ _enableAsyncRouteHandlers,
999
+ );
1000
+
1001
+ getCurrentScope().setTransactionName(name || '/');
1002
+
1003
+ if (activeRootSpan) {
1004
+ activeRootSpan.updateName(name);
1005
+ activeRootSpan.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
1006
+
1007
+ // Patch span.end() to ensure we update the name one last time before the span is sent
1008
+ patchSpanEnd(activeRootSpan, location, routes, basename, 'pageload');
1009
+ }
1010
+ } else if (activeRootSpan) {
1011
+ // Even if branches is null (can happen when lazy routes haven't loaded yet),
1012
+ // we still need to patch span.end() so that when lazy routes load and the span ends,
1013
+ // we can update the transaction name correctly.
1014
+ patchSpanEnd(activeRootSpan, location, routes, basename, 'pageload');
1015
+ }
1016
+ }
1017
+
1018
+ /**
1019
+ * Determines if a span name should be updated during wildcard route resolution.
1020
+ *
1021
+ * Update conditions (in priority order):
1022
+ * 1. No current name + allowNoCurrentName: true → always update (pageload spans)
1023
+ * 2. Current name has wildcard + new is route without wildcard → upgrade (e.g., "/users/*" → "/users/:id")
1024
+ * 3. Current source is not 'route' + new source is 'route' → upgrade (e.g., URL → parameterized route)
1025
+ *
1026
+ * @param currentName - The current span name (may be undefined)
1027
+ * @param currentSource - The current span source ('route', 'url', or undefined)
1028
+ * @param newName - The proposed new span name
1029
+ * @param newSource - The proposed new span source
1030
+ * @param allowNoCurrentName - If true, allow updates when there's no current name (for pageload spans)
1031
+ * @returns true if the span name should be updated
1032
+ */
1033
+ function shouldUpdateWildcardSpanName(
1034
+ currentName,
1035
+ currentSource,
1036
+ newName,
1037
+ newSource,
1038
+ allowNoCurrentName = false,
1039
+ ) {
1040
+ if (!newName) {
1041
+ return false;
1042
+ }
1043
+
1044
+ if (!currentName && allowNoCurrentName) {
1045
+ return true;
1046
+ }
1047
+
1048
+ const hasWildcard = currentName && transactionNameHasWildcard(currentName);
1049
+
1050
+ if (hasWildcard && newSource === 'route' && !transactionNameHasWildcard(newName)) {
1051
+ return true;
1052
+ }
1053
+
1054
+ if (currentSource !== 'route' && newSource === 'route') {
1055
+ return true;
1056
+ }
1057
+
1058
+ return false;
1059
+ }
1060
+
1061
+ function tryUpdateSpanNameBeforeEnd(
1062
+ span,
1063
+ spanJson,
1064
+ currentName,
1065
+ location,
1066
+ routes,
1067
+ basename,
1068
+ spanType,
1069
+ allRoutes,
1070
+ ) {
1071
+ try {
1072
+ const currentSource = spanJson.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
1073
+
1074
+ if (currentSource === 'route' && currentName && !transactionNameHasWildcard(currentName)) {
1075
+ return;
1076
+ }
1077
+
1078
+ const currentAllRoutes = Array.from(allRoutes);
1079
+ const routesToUse = currentAllRoutes.length > 0 ? currentAllRoutes : routes;
1080
+ const branches = _matchRoutes(routesToUse, location, basename) ;
1081
+
1082
+ if (!branches) {
1083
+ return;
1084
+ }
1085
+
1086
+ const [name, source] = resolveRouteNameAndSource(
1087
+ location,
1088
+ routesToUse,
1089
+ routesToUse,
1090
+ branches,
1091
+ basename,
1092
+ _lazyRouteManifest,
1093
+ _enableAsyncRouteHandlers,
1094
+ );
1095
+
1096
+ const isImprovement = shouldUpdateWildcardSpanName(currentName, currentSource, name, source, true);
1097
+ const spanNotEnded = spanType === 'pageload' || !spanJson.timestamp;
1098
+
1099
+ if (isImprovement && spanNotEnded) {
1100
+ span.updateName(name);
1101
+ span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, source);
1102
+ }
1103
+ } catch (error) {
1104
+ DEBUG_BUILD && debug.warn(`Error updating span details before ending: ${error}`);
1105
+ }
1106
+ }
1107
+
1108
+ /**
1109
+ * Patches the span.end() method to update the transaction name one last time before the span is sent.
1110
+ * This handles cases where the span is cancelled early (e.g., document.hidden) before lazy routes have finished loading.
1111
+ */
1112
+ function patchSpanEnd(
1113
+ span,
1114
+ location,
1115
+ routes,
1116
+ basename,
1117
+ spanType,
1118
+ ) {
1119
+ const patchedPropertyName = `__sentry_${spanType}_end_patched__` ;
1120
+ const hasEndBeenPatched = (span )?.[patchedPropertyName];
1121
+
1122
+ if (hasEndBeenPatched || !span.end) {
1123
+ return;
1124
+ }
1125
+
1126
+ // Uses global allRoutes to access lazy-loaded routes added after this function was called.
1127
+
1128
+ const originalEnd = span.end.bind(span);
1129
+ let endCalled = false;
1130
+
1131
+ span.end = function patchedEnd(...args) {
1132
+ if (endCalled) {
1133
+ return;
1134
+ }
1135
+ endCalled = true;
1136
+
1137
+ // Capture timestamp immediately to avoid delay from async operations
1138
+ // If no timestamp was provided, capture the current time now
1139
+ const endTimestamp = args.length > 0 ? args[0] : Date.now() / 1000;
1140
+
1141
+ const spanJson = spanToJSON(span);
1142
+ const currentName = spanJson.description;
1143
+ const currentSource = spanJson.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
1144
+
1145
+ // Helper to clean up activeNavigationSpans after span ends
1146
+ const cleanupNavigationSpan = () => {
1147
+ const client = getClient();
1148
+ if (client && spanType === 'navigation') {
1149
+ const trackedNav = activeNavigationSpans.get(client);
1150
+ if (trackedNav && trackedNav.span === span) {
1151
+ activeNavigationSpans.delete(client);
1152
+ }
1153
+ }
1154
+ };
1155
+
1156
+ const pendingPromises = pendingLazyRouteLoads.get(span);
1157
+ const mayHaveLazyRoutes = (span ).__sentry_may_have_lazy_routes__;
1158
+
1159
+ // Wait for lazy routes if:
1160
+ // 1. (There are pending promises OR the span was marked as potentially having lazy routes) AND
1161
+ // 2. Current name exists AND
1162
+ // 3. Either the name has a wildcard OR the source is not 'route' (URL-based names)
1163
+ const hasPendingOrMayHaveLazyRoutes = (pendingPromises && pendingPromises.size > 0) || mayHaveLazyRoutes;
1164
+ const shouldWaitForLazyRoutes =
1165
+ hasPendingOrMayHaveLazyRoutes &&
1166
+ currentName &&
1167
+ (transactionNameHasWildcard(currentName) || currentSource !== 'route');
1168
+
1169
+ if (shouldWaitForLazyRoutes) {
1170
+ if (_lazyRouteTimeout === 0) {
1171
+ tryUpdateSpanNameBeforeEnd(span, spanJson, currentName, location, routes, basename, spanType, allRoutes);
1172
+ cleanupNavigationSpan();
1173
+ originalEnd(endTimestamp);
1174
+ return;
1175
+ }
1176
+
1177
+ // If we have pending promises, wait for them. Otherwise, just wait for the timeout.
1178
+ // This handles the case where we know lazy routes might load but patchRoutesOnNavigation
1179
+ // hasn't been called yet.
1180
+ const timeoutPromise = new Promise(r => setTimeout(r, _lazyRouteTimeout));
1181
+ let waitPromise;
1182
+
1183
+ if (pendingPromises && pendingPromises.size > 0) {
1184
+ const allSettled = Promise.allSettled(pendingPromises).then(() => {});
1185
+ waitPromise = _lazyRouteTimeout === Infinity ? allSettled : Promise.race([allSettled, timeoutPromise]);
1186
+ } else {
1187
+ // No pending promises yet, but we know lazy routes might load
1188
+ // Wait for the timeout to give React Router time to call patchRoutesOnNavigation
1189
+ waitPromise = timeoutPromise;
1190
+ }
1191
+
1192
+ waitPromise
1193
+ .then(() => {
1194
+ const updatedSpanJson = spanToJSON(span);
1195
+ tryUpdateSpanNameBeforeEnd(
1196
+ span,
1197
+ updatedSpanJson,
1198
+ updatedSpanJson.description,
1199
+ location,
1200
+ routes,
1201
+ basename,
1202
+ spanType,
1203
+ allRoutes,
1204
+ );
1205
+ cleanupNavigationSpan();
1206
+ originalEnd(endTimestamp);
1207
+ })
1208
+ .catch(() => {
1209
+ cleanupNavigationSpan();
1210
+ originalEnd(endTimestamp);
1211
+ });
1212
+ return;
1213
+ }
1214
+
1215
+ tryUpdateSpanNameBeforeEnd(span, spanJson, currentName, location, routes, basename, spanType, allRoutes);
1216
+ cleanupNavigationSpan();
1217
+ originalEnd(endTimestamp);
1218
+ };
1219
+
1220
+ addNonEnumerableProperty(span , patchedPropertyName, true);
1221
+ }
1222
+
1223
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1224
+ function createV6CompatibleWithSentryReactRouterRouting(
1225
+ Routes,
1226
+ version,
1227
+ ) {
1228
+ if (!_useEffect || !_useLocation || !_useNavigationType || !_createRoutesFromChildren || !_matchRoutes) {
1229
+ DEBUG_BUILD &&
1230
+ debug.warn(`reactRouterV6Instrumentation was unable to wrap Routes because of one or more missing parameters.
1231
+ useEffect: ${_useEffect}. useLocation: ${_useLocation}. useNavigationType: ${_useNavigationType}.
1232
+ createRoutesFromChildren: ${_createRoutesFromChildren}. matchRoutes: ${_matchRoutes}.`);
1233
+
1234
+ return Routes;
1235
+ }
1236
+
1237
+ const SentryRoutes = (props) => {
1238
+ const isMountRenderPass = React.useRef(true);
1239
+
1240
+ const location = _useLocation();
1241
+ const navigationType = _useNavigationType();
1242
+
1243
+ _useEffect(
1244
+ () => {
1245
+ const routes = _createRoutesFromChildren(props.children) ;
1246
+
1247
+ if (isMountRenderPass.current) {
1248
+ addRoutesToAllRoutes(routes);
1249
+
1250
+ updatePageloadTransaction({
1251
+ activeRootSpan: getActiveRootSpan(),
1252
+ location,
1253
+ routes,
1254
+ allRoutes: Array.from(allRoutes),
1255
+ });
1256
+ isMountRenderPass.current = false;
1257
+ } else {
1258
+ // Note: Component-based routes don't support lazy route tracking via lazyRouteTimeout
1259
+ // because React.lazy() loads happen at the component level, not the router level.
1260
+ // Use createBrowserRouter with patchRoutesOnNavigation for lazy route tracking.
1261
+ handleNavigation({ location, routes, navigationType, version, allRoutes: Array.from(allRoutes) });
1262
+ }
1263
+ },
1264
+ // Re-run only on location/navigation changes, not children changes
1265
+ [location, navigationType],
1266
+ );
1267
+
1268
+ // @ts-expect-error Setting more specific React Component typing for `R` generic above
1269
+ // will break advanced type inference done by react router params
1270
+ return React.createElement(Routes, { ...props,} );
1271
+ };
1272
+
1273
+ hoistNonReactStatics(SentryRoutes, Routes);
1274
+
1275
+ // @ts-expect-error Setting more specific React Component typing for `R` generic above
1276
+ // will break advanced type inference done by react router params
1277
+ return SentryRoutes;
1278
+ }
1279
+
1280
+ export { addResolvedRoutesToParent, addRoutesToAllRoutes, allRoutes, computeLocationKey, createReactRouterV6CompatibleTracingIntegration, createV6CompatibleWithSentryReactRouterRouting, createV6CompatibleWrapCreateBrowserRouter, createV6CompatibleWrapCreateMemoryRouter, createV6CompatibleWrapUseRoutes, handleNavigation, processResolvedRoutes, shouldSkipNavigation, updateNavigationSpan };
1281
+ //# sourceMappingURL=instrumentation.js.map