|
| 1 | +import { startTransition, StrictMode } from "react"; |
| 2 | +import { hydrateRoot } from "react-dom/client"; |
| 3 | +import type { |
| 4 | + DataRouteObject, |
| 5 | + DataRouter, |
| 6 | + RouterNavigateOptions, |
| 7 | +} from "react-router"; |
| 8 | +import { HydratedRouter } from "react-router/dom"; |
| 9 | +import { getPattern, measure, startMeasure } from "./o11y"; |
| 10 | + |
| 11 | +startTransition(() => { |
| 12 | + hydrateRoot( |
| 13 | + document, |
| 14 | + <StrictMode> |
| 15 | + <HydratedRouter |
| 16 | + unstable_instrumentRoute={instrumentRoute} |
| 17 | + unstable_instrumentRouter={instrumentRouter} |
| 18 | + /> |
| 19 | + </StrictMode>, |
| 20 | + ); |
| 21 | +}); |
| 22 | + |
| 23 | +function instrumentRouter(router: DataRouter) { |
| 24 | + let initialize = router.initialize; |
| 25 | + router.initialize = () => { |
| 26 | + let pattern = getPattern(router.routes, router.state.location.pathname); |
| 27 | + let end = startMeasure(["initialize", pattern]); |
| 28 | + |
| 29 | + if (router.state.initialized) { |
| 30 | + end(); |
| 31 | + } else { |
| 32 | + let unsubscribe = router.subscribe((state) => { |
| 33 | + if (state.initialized) { |
| 34 | + end(); |
| 35 | + unsubscribe(); |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
| 39 | + return initialize(); |
| 40 | + }; |
| 41 | + |
| 42 | + let navigate = router.navigate; |
| 43 | + router.navigate = async (to, opts?: RouterNavigateOptions) => { |
| 44 | + let path = |
| 45 | + typeof to === "string" |
| 46 | + ? to |
| 47 | + : typeof to === "number" |
| 48 | + ? String(to) |
| 49 | + : (to?.pathname ?? "unknown"); |
| 50 | + await measure([`navigate`, getPattern(router.routes, path)], () => |
| 51 | + typeof to === "number" ? navigate(to) : navigate(to, opts), |
| 52 | + ); |
| 53 | + }; |
| 54 | + |
| 55 | + return router; |
| 56 | +} |
| 57 | + |
| 58 | +function instrumentRoute(route: DataRouteObject): DataRouteObject { |
| 59 | + if (typeof route.lazy === "function") { |
| 60 | + let lazy = route.lazy; |
| 61 | + route.lazy = () => measure(["lazy", route.id], () => lazy()); |
| 62 | + } |
| 63 | + |
| 64 | + if ( |
| 65 | + route.middleware && |
| 66 | + route.middleware.length > 0 && |
| 67 | + // @ts-expect-error |
| 68 | + route.middleware.instrumented !== true |
| 69 | + ) { |
| 70 | + route.middleware = route.middleware.map((mw, i) => { |
| 71 | + return ({ request, params, pattern, context }, next) => |
| 72 | + measure(["middleware", route.id, i.toString(), pattern], async () => |
| 73 | + mw({ request, params, pattern, context }, next), |
| 74 | + ); |
| 75 | + }); |
| 76 | + // When `route.lazy` is used alongside a statically defined `loader`, make |
| 77 | + // sure we don't double-instrument the `loader` after `route.lazy` completes |
| 78 | + // and we re-call `instrumentRoute` via `mapRouteProperties` |
| 79 | + // @ts-expect-error |
| 80 | + route.middleware.instrumented = true; |
| 81 | + } |
| 82 | + |
| 83 | + // @ts-expect-error |
| 84 | + if (typeof route.loader === "function" && !route.loader.instrumented) { |
| 85 | + let loader = route.loader; |
| 86 | + route.loader = (...args) => { |
| 87 | + return measure([`loader:${route.id}`, args[0].pattern], async () => |
| 88 | + loader(...args), |
| 89 | + ); |
| 90 | + }; |
| 91 | + // When `route.lazy` is used alongside a statically defined `loader`, make |
| 92 | + // sure we don't double-instrument the `loader` after `route.lazy` completes |
| 93 | + // and we re-call `instrumentRoute` via `mapRouteProperties` |
| 94 | + // @ts-expect-error |
| 95 | + route.loader.instrumented = true; |
| 96 | + } |
| 97 | + |
| 98 | + return route; |
| 99 | +} |
0 commit comments