Skip to content

Commit 2be4003

Browse files
committed
Use history listen instead of monkeypatch
1 parent f541bc6 commit 2be4003

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

frontend/src/datadog.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,41 @@
44

55
import { datadogRum } from '@datadog/browser-rum';
66

7+
// --- Datadog RUM + React Router v5 wiring (no monkeypatching) ---
8+
import type { Location } from 'history';
9+
10+
function pathFromLocation(
11+
loc: Location | { pathname: string; search?: string; hash?: string }
12+
): string {
13+
const search = loc.search ?? '';
14+
const hash = loc.hash ?? '';
15+
return `${loc.pathname}${search}${hash}`;
16+
}
17+
18+
/**
19+
* Call this once, right after you create your app's history and before rendering the <Router>.
20+
* Example:
21+
* // history.ts
22+
* export const history = createBrowserHistory();
23+
*
24+
* // index.tsx
25+
* import { history } from './history';
26+
* import { attachRumToHistory } from './datadog';
27+
* attachRumToHistory(history);
28+
*/
29+
export function attachRumToHistory(history: {
30+
location: Location;
31+
listen: (cb: (loc: Location) => void) => void;
32+
}): void {
33+
// Start initial view
34+
datadogRum.startView({ name: pathFromLocation(history.location) });
35+
// Track subsequent route changes
36+
history.listen((location) => {
37+
datadogRum.startView({ name: pathFromLocation(location) });
38+
});
39+
}
40+
// --- End Datadog RUM + React Router v5 wiring ---
41+
742
datadogRum.init({
843
applicationId: 'dfabd184-b8ed-47db-90bc-2a4c26828b72',
944
clientToken: 'pub2b91b53e324deaa20ce2c3738fc4f9aa',
@@ -15,38 +50,3 @@ datadogRum.init({
1550
defaultPrivacyLevel: 'mask-user-input',
1651
// plugins: [reactPlugin({ router: false })], // Keep disabled until upgrading React Router due to deps conflict
1752
});
18-
19-
// --- Datadog RUM manual SPA view tracking for React Router v5 ---
20-
function currentPath(): string {
21-
return (
22-
window.location.pathname + window.location.search + window.location.hash
23-
);
24-
}
25-
26-
// Start the initial view
27-
datadogRum.startView({ name: currentPath() });
28-
29-
// Patch pushState and replaceState to detect programmatic navigations
30-
(function patchHistoryForRum() {
31-
const originalPushState = history.pushState;
32-
const originalReplaceState = history.replaceState;
33-
34-
history.pushState = function (...args) {
35-
const ret = originalPushState.apply(this, args as any);
36-
// Defer to allow location to update
37-
queueMicrotask(() => datadogRum.startView({ name: currentPath() }));
38-
return ret;
39-
} as typeof history.pushState;
40-
41-
history.replaceState = function (...args) {
42-
const ret = originalReplaceState.apply(this, args as any);
43-
queueMicrotask(() => datadogRum.startView({ name: currentPath() }));
44-
return ret;
45-
} as typeof history.replaceState;
46-
47-
// Back/forward buttons
48-
window.addEventListener('popstate', () => {
49-
datadogRum.startView({ name: currentPath() });
50-
});
51-
})();
52-
// --- End Datadog RUM manual SPA view tracking ---
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { createBrowserHistory } from 'history';
2+
import { attachRumToHistory } from '../../datadog';
23

34
const history = createBrowserHistory();
45

6+
attachRumToHistory(history);
7+
58
export default history;

0 commit comments

Comments
 (0)