Skip to content

Commit 3473447

Browse files
authored
fix(react): Add React Router Descendant Routes support. (#14304)
1 parent 2cff1f7 commit 3473447

File tree

18 files changed

+798
-750
lines changed

18 files changed

+798
-750
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
24+
25+
/test-results/
26+
/playwright-report/
27+
/playwright/.cache/
28+
29+
!*.d.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@sentry:registry=http://127.0.0.1:4873
2+
@sentry-internal:registry=http://127.0.0.1:4873
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "react-router-6-descendant-routes",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@sentry/react": "latest || *",
7+
"@types/react": "18.0.0",
8+
"@types/react-dom": "18.0.0",
9+
"express": "4.19.2",
10+
"react": "18.2.0",
11+
"react-dom": "18.2.0",
12+
"react-router-dom": "^6.28.0",
13+
"react-scripts": "5.0.1",
14+
"typescript": "4.9.5"
15+
},
16+
"scripts": {
17+
"build": "react-scripts build",
18+
"start": "run-p start:client start:server",
19+
"start:client": "node server/app.js",
20+
"start:server": "serve -s build",
21+
"test": "playwright test",
22+
"clean": "npx rimraf node_modules pnpm-lock.yaml",
23+
"test:build": "pnpm install && npx playwright install && pnpm build",
24+
"test:build-ts3.8": "pnpm install && pnpm add [email protected] && npx playwright install && pnpm build",
25+
"test:build-canary": "pnpm install && pnpm add react@canary react-dom@canary && npx playwright install && pnpm build",
26+
"test:assert": "pnpm test"
27+
},
28+
"eslintConfig": {
29+
"extends": [
30+
"react-app",
31+
"react-app/jest"
32+
]
33+
},
34+
"browserslist": {
35+
"production": [
36+
">0.2%",
37+
"not dead",
38+
"not op_mini all"
39+
],
40+
"development": [
41+
"last 1 chrome version",
42+
"last 1 firefox version",
43+
"last 1 safari version"
44+
]
45+
},
46+
"devDependencies": {
47+
"@playwright/test": "^1.44.1",
48+
"@sentry-internal/test-utils": "link:../../../test-utils",
49+
"serve": "14.0.1",
50+
"npm-run-all2": "^6.2.0"
51+
},
52+
"volta": {
53+
"extends": "../../package.json"
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getPlaywrightConfig } from '@sentry-internal/test-utils';
2+
3+
const config = getPlaywrightConfig({
4+
startCommand: `pnpm start`,
5+
});
6+
7+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<meta name="theme-color" content="#000000" />
7+
<meta name="description" content="Web site created using create-react-app" />
8+
<title>React App</title>
9+
</head>
10+
<body>
11+
<noscript>You need to enable JavaScript to run this app.</noscript>
12+
<div id="root"></div>
13+
<!--
14+
This HTML file is a template.
15+
If you open it directly in the browser, you will see an empty page.
16+
17+
You can add webfonts, meta tags, or analytics to this file.
18+
The build step will place the bundled scripts into the <body> tag.
19+
20+
To begin the development, run `npm start` or `yarn start`.
21+
To create a production bundle, use `npm run build` or `yarn build`.
22+
-->
23+
</body>
24+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const express = require('express');
2+
3+
const app = express();
4+
const PORT = 8080;
5+
6+
const wait = time => {
7+
return new Promise(resolve => {
8+
setTimeout(() => {
9+
resolve();
10+
}, time);
11+
});
12+
};
13+
14+
async function sseHandler(request, response, timeout = false) {
15+
response.headers = {
16+
'Content-Type': 'text/event-stream',
17+
Connection: 'keep-alive',
18+
'Cache-Control': 'no-cache',
19+
'Access-Control-Allow-Origin': '*',
20+
};
21+
22+
response.setHeader('Cache-Control', 'no-cache');
23+
response.setHeader('Content-Type', 'text/event-stream');
24+
response.setHeader('Access-Control-Allow-Origin', '*');
25+
response.setHeader('Connection', 'keep-alive');
26+
27+
response.flushHeaders();
28+
29+
await wait(2000);
30+
31+
for (let index = 0; index < 10; index++) {
32+
response.write(`data: ${new Date().toISOString()}\n\n`);
33+
if (timeout) {
34+
await wait(10000);
35+
}
36+
}
37+
38+
response.end();
39+
}
40+
41+
app.get('/sse', (req, res) => sseHandler(req, res));
42+
43+
app.get('/sse-timeout', (req, res) => sseHandler(req, res, true));
44+
45+
app.listen(PORT, () => {
46+
console.log(`SSE service listening at http://localhost:${PORT}`);
47+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
interface Window {
2+
recordedTransactions?: string[];
3+
capturedExceptionId?: string;
4+
sentryReplayId?: string;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as Sentry from '@sentry/react';
2+
import React from 'react';
3+
import ReactDOM from 'react-dom/client';
4+
import {
5+
BrowserRouter,
6+
Route,
7+
Routes,
8+
createRoutesFromChildren,
9+
matchRoutes,
10+
useLocation,
11+
useNavigationType,
12+
} from 'react-router-dom';
13+
import Index from './pages/Index';
14+
15+
const replay = Sentry.replayIntegration();
16+
17+
Sentry.init({
18+
environment: 'qa', // dynamic sampling bias to keep transactions
19+
dsn: process.env.REACT_APP_E2E_TEST_DSN,
20+
integrations: [
21+
Sentry.reactRouterV6BrowserTracingIntegration({
22+
useEffect: React.useEffect,
23+
useLocation,
24+
useNavigationType,
25+
createRoutesFromChildren,
26+
matchRoutes,
27+
trackFetchStreamPerformance: true,
28+
}),
29+
replay,
30+
],
31+
// We recommend adjusting this value in production, or using tracesSampler
32+
// for finer control
33+
tracesSampleRate: 1.0,
34+
release: 'e2e-test',
35+
36+
// Always capture replays, so we can test this properly
37+
replaysSessionSampleRate: 1.0,
38+
replaysOnErrorSampleRate: 0.0,
39+
40+
tunnel: 'http://localhost:3031',
41+
});
42+
43+
const SentryRoutes = Sentry.withSentryReactRouterV6Routing(Routes);
44+
45+
const DetailsRoutes = () => (
46+
<SentryRoutes>
47+
<Route path=":detailId" element={<div id="details">Details</div>} />
48+
</SentryRoutes>
49+
);
50+
51+
const ViewsRoutes = () => (
52+
<SentryRoutes>
53+
<Route index element={<div id="views">Views</div>} />
54+
<Route path="views/:viewId/*" element={<DetailsRoutes />} />
55+
</SentryRoutes>
56+
);
57+
58+
const ProjectsRoutes = () => (
59+
<SentryRoutes>
60+
<Route path="projects/:projectId/*" element={<ViewsRoutes />}></Route>
61+
<Route path="*" element={<div>No Match Page</div>} />
62+
</SentryRoutes>
63+
);
64+
65+
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
66+
root.render(
67+
<BrowserRouter>
68+
<SentryRoutes>
69+
<Route path="/" element={<Index />} />
70+
<Route path="/*" element={<ProjectsRoutes />}></Route>
71+
</SentryRoutes>
72+
</BrowserRouter>,
73+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// biome-ignore lint/nursery/noUnusedImports: Need React import for JSX
2+
import * as React from 'react';
3+
import { Link } from 'react-router-dom';
4+
5+
const Index = () => {
6+
return (
7+
<>
8+
<Link to="/projects/123/views/456/789" id="navigation">
9+
navigate
10+
</Link>
11+
</>
12+
);
13+
};
14+
15+
export default Index;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="react-scripts" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { startEventProxyServer } from '@sentry-internal/test-utils';
2+
3+
startEventProxyServer({
4+
port: 3031,
5+
proxyServerName: 'react-router-6-descendant-routes',
6+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForTransaction } from '@sentry-internal/test-utils';
3+
4+
test('sends a pageload transaction with a parameterized URL', async ({ page }) => {
5+
const transactionPromise = waitForTransaction('react-router-6-descendant-routes', async transactionEvent => {
6+
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
7+
});
8+
9+
await page.goto(`/projects/123/views/234/567`);
10+
11+
const rootSpan = await transactionPromise;
12+
13+
expect(rootSpan).toMatchObject({
14+
contexts: {
15+
trace: {
16+
op: 'pageload',
17+
origin: 'auto.pageload.react.reactrouter_v6',
18+
},
19+
},
20+
transaction: '/projects/:projectId/views/:viewId/:detailId',
21+
transaction_info: {
22+
source: 'route',
23+
},
24+
});
25+
});
26+
27+
test('sends a navigation transaction with a parameterized URL', async ({ page }) => {
28+
const pageloadTxnPromise = waitForTransaction('react-router-6-descendant-routes', async transactionEvent => {
29+
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'pageload';
30+
});
31+
32+
const navigationTxnPromise = waitForTransaction('react-router-6-descendant-routes', async transactionEvent => {
33+
return !!transactionEvent?.transaction && transactionEvent.contexts?.trace?.op === 'navigation';
34+
});
35+
36+
await page.goto(`/`);
37+
const pageloadTxn = await pageloadTxnPromise;
38+
39+
expect(pageloadTxn).toMatchObject({
40+
contexts: {
41+
trace: {
42+
op: 'pageload',
43+
origin: 'auto.pageload.react.reactrouter_v6',
44+
},
45+
},
46+
transaction: '/',
47+
transaction_info: {
48+
source: 'route',
49+
},
50+
});
51+
52+
const linkElement = page.locator('id=navigation');
53+
54+
const [_, navigationTxn] = await Promise.all([linkElement.click(), navigationTxnPromise]);
55+
expect(navigationTxn).toMatchObject({
56+
contexts: {
57+
trace: {
58+
op: 'navigation',
59+
origin: 'auto.navigation.react.reactrouter_v6',
60+
},
61+
},
62+
transaction: '/projects/:projectId/views/:viewId/:detailId',
63+
transaction_info: {
64+
source: 'route',
65+
},
66+
});
67+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2018",
4+
"lib": ["dom", "dom.iterable", "esnext"],
5+
"allowJs": true,
6+
"skipLibCheck": true,
7+
"esModuleInterop": true,
8+
"allowSyntheticDefaultImports": true,
9+
"strict": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"noFallthroughCasesInSwitch": true,
12+
"module": "esnext",
13+
"moduleResolution": "node",
14+
"resolveJsonModule": true,
15+
"isolatedModules": true,
16+
"noEmit": true,
17+
"jsx": "react"
18+
},
19+
"include": ["src", "tests"]
20+
}

packages/react/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@
6767
"react-router-3": "npm:[email protected]",
6868
"react-router-4": "npm:[email protected]",
6969
"react-router-5": "npm:[email protected]",
70-
"react-router-6": "npm:[email protected]",
71-
"react-router-6.4": "npm:[email protected]",
70+
"react-router-6": "npm:[email protected]",
7271
"redux": "^4.0.5"
7372
},
7473
"scripts": {

0 commit comments

Comments
 (0)