-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(react): Add @sentry/react/react-router entry with automatic React Router hooks
#24014
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
17b28f3
feat(react): Add `@sentry/react/router` entry with default React Rout…
mydea 7bcf8e9
refactor(react): Thread React Router hooks through the call chain
mydea 268ac27
refactor(react): Store React Router config on the client instead of m…
mydea e70e456
test(e2e): Add `@sentry/react/router` migration entry and e2e app
mydea 4b3768e
cleanup comments
mydea de388e3
Apply batched suggestions from code review
mydea 44c27dc
adjust migration guide
mydea 47699d0
test(e2e): Add `@sentry/react/router` e2e apps for React Router v6 an…
mydea 0cf62e7
dedupe deps
mydea 784b8ad
test(e2e): Type-check the react-router-7-router-entry app
mydea 0816d2e
test(e2e): Wrap routes before init in the v7 router-entry app
mydea 44e28ce
fix exports
mydea d79aa58
feat(react): Rename `/router` entry point to `/react-router`
mydea 960047d
fix it
mydea 0896802
prevent remount
mydea bca9fd7
Apply suggestion from @chargome
mydea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
|
|
||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| /test-results/ | ||
| /playwright-report/ | ||
| /playwright/.cache/ | ||
|
|
||
| !*.d.ts |
13 changes: 13 additions & 0 deletions
13
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Vite + React + TS</title> | ||
| </head> | ||
| <body> | ||
| <div id="root"></div> | ||
| <script type="module" src="/src/main.tsx"></script> | ||
| </body> | ||
| </html> |
52 changes: 52 additions & 0 deletions
52
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| { | ||
| "name": "react-router-6-router-entry", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "@sentry/react": "file:../../packed/sentry-react-packed.tgz", | ||
| "@types/react": "18.3.1", | ||
| "@types/react-dom": "18.3.1", | ||
| "react": "18.3.1", | ||
| "react-dom": "18.3.1", | ||
| "react-router-dom": "^6.30.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@playwright/test": "~1.56.0", | ||
| "@sentry-internal/test-utils": "link:../../../test-utils", | ||
| "vite": "^6.4.2", | ||
| "@vitejs/plugin-react": "^4.3.4", | ||
| "typescript": "~5.0.0" | ||
| }, | ||
| "scripts": { | ||
| "build": "vite build", | ||
| "dev": "vite", | ||
| "preview": "vite preview", | ||
| "test": "playwright test", | ||
| "clean": "npx rimraf node_modules pnpm-lock.yaml", | ||
| "typecheck": "tsc --noEmit", | ||
| "test:build": "pnpm install && pnpm build", | ||
| "test:build-canary": "pnpm install && pnpm add react@canary react-dom@canary && pnpm build", | ||
| "test:assert": "pnpm typecheck && pnpm test" | ||
| }, | ||
| "eslintConfig": { | ||
| "extends": [ | ||
| "react-app", | ||
| "react-app/jest" | ||
| ] | ||
| }, | ||
| "browserslist": { | ||
| "production": [ | ||
| ">0.2%", | ||
| "not dead", | ||
| "not op_mini all" | ||
| ], | ||
| "development": [ | ||
| "last 1 chrome version", | ||
| "last 1 firefox version", | ||
| "last 1 safari version" | ||
| ] | ||
| }, | ||
| "volta": { | ||
| "extends": "../../package.json" | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/playwright.config.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
|
||
| const config = getPlaywrightConfig({ | ||
| startCommand: `pnpm preview --port 3030`, | ||
| port: 3030, | ||
| }); | ||
|
|
||
| export default config; |
5 changes: 5 additions & 0 deletions
5
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/src/globals.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| interface Window { | ||
| recordedTransactions?: string[]; | ||
| capturedExceptionId?: string; | ||
| sentryReplayId?: string; | ||
| } |
35 changes: 35 additions & 0 deletions
35
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/src/main.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import * as Sentry from '@sentry/react'; | ||
| // The `@sentry/react/react-router` entry pulls the required router hooks from `react-router` itself, so | ||
| // `reactRouterBrowserTracingIntegration()` needs no arguments. On React Router v6 the DOM bindings | ||
| // (`BrowserRouter`, `Link`) come from `react-router-dom`. Note this app depends only on | ||
| // `react-router-dom` (not `react-router` directly) - the entry's `react-router` import still resolves | ||
| // via the copy `react-router-dom` pulls in, which is the common real-world v6 setup. | ||
| import { reactRouterBrowserTracingIntegration, wrapReactRouterRouting } from '@sentry/react/react-router'; | ||
| import * as React from 'react'; | ||
| import ReactDOM from 'react-dom/client'; | ||
| import { BrowserRouter, Route, Routes } from 'react-router-dom'; | ||
| import Index from './pages/Index'; | ||
| import Products from './pages/Products'; | ||
| import User from './pages/User'; | ||
|
|
||
| Sentry.init({ | ||
| environment: 'qa', // dynamic sampling bias to keep transactions | ||
| dsn: import.meta.env.PUBLIC_E2E_TEST_DSN, | ||
| integrations: [reactRouterBrowserTracingIntegration()], | ||
| tracesSampleRate: 1.0, | ||
| release: 'e2e-test', | ||
| tunnel: 'http://localhost:3031', | ||
| }); | ||
|
|
||
| const SentryRoutes = wrapReactRouterRouting(Routes); | ||
|
|
||
| const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement); | ||
| root.render( | ||
| <BrowserRouter> | ||
| <SentryRoutes> | ||
| <Route path="/" element={<Index />} /> | ||
| <Route path="/user/:id" element={<User />} /> | ||
| <Route path="/products" element={<Products />} /> | ||
| </SentryRoutes> | ||
| </BrowserRouter>, | ||
| ); |
25 changes: 25 additions & 0 deletions
25
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/src/pages/Index.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import * as React from 'react'; | ||
| import { Link } from 'react-router-dom'; | ||
|
|
||
| const Index = () => { | ||
| return ( | ||
| <> | ||
| <input | ||
| type="button" | ||
| value="Capture Exception" | ||
| id="exception-button" | ||
| onClick={() => { | ||
| throw new Error('I am an error!'); | ||
| }} | ||
| /> | ||
| <Link to="/user/5" id="navigation"> | ||
| navigate | ||
| </Link> | ||
| <Link to="/products" id="navigation-products"> | ||
| products | ||
| </Link> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default Index; |
16 changes: 16 additions & 0 deletions
16
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/src/pages/Products.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| const Products = () => { | ||
| // Fired on mount, i.e. while navigating to /products. This mirrors a typical | ||
| // route component that loads its data in an effect. The request is same-origin, | ||
| // so the SDK attaches `sentry-trace`/`baggage` headers by default. | ||
| React.useEffect(() => { | ||
| fetch('/api/products').catch(() => { | ||
| // ignore network errors in the test environment | ||
| }); | ||
| }, []); | ||
|
|
||
| return <div id="products">Products</div>; | ||
| }; | ||
|
|
||
| export default Products; |
7 changes: 7 additions & 0 deletions
7
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/src/pages/User.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| const User = () => { | ||
| return <p>I am a blank page :)</p>; | ||
| }; | ||
|
|
||
| export default User; |
6 changes: 6 additions & 0 deletions
6
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/start-event-proxy.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { startEventProxyServer } from '@sentry-internal/test-utils'; | ||
|
|
||
| startEventProxyServer({ | ||
| port: 3031, | ||
| proxyServerName: 'react-router-6-router-entry', | ||
| }); |
59 changes: 59 additions & 0 deletions
59
dev-packages/e2e-tests/test-applications/react-router-6-router-entry/tests/errors.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { getSpanOp, waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils'; | ||
|
|
||
| test('Sends correct error event', async ({ page }) => { | ||
| const errorEventPromise = waitForError('react-router-6-router-entry', event => { | ||
| return !event.type && event.exception?.values?.[0]?.value === 'I am an error!'; | ||
| }); | ||
|
|
||
| await page.goto('/'); | ||
|
|
||
| const exceptionButton = page.locator('id=exception-button'); | ||
| await exceptionButton.click(); | ||
|
|
||
| const errorEvent = await errorEventPromise; | ||
|
|
||
| expect(errorEvent.exception?.values).toHaveLength(1); | ||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('I am an error!'); | ||
|
|
||
| expect(errorEvent.request).toEqual({ | ||
| headers: expect.any(Object), | ||
| url: 'http://localhost:3030/', | ||
| }); | ||
|
|
||
| expect(errorEvent.transaction).toEqual('/'); | ||
|
|
||
| expect(errorEvent.contexts?.trace).toEqual({ | ||
| trace_id: expect.any(String), | ||
| span_id: expect.any(String), | ||
| }); | ||
| }); | ||
|
|
||
| test('Sets correct transactionName', async ({ page }) => { | ||
| const pageloadSpanPromise = waitForStreamedSpan('react-router-6-router-entry', span => { | ||
| return getSpanOp(span) === 'pageload' && span.is_segment; | ||
| }); | ||
|
|
||
| const errorEventPromise = waitForError('react-router-6-router-entry', event => { | ||
| return !event.type && event.exception?.values?.[0]?.value === 'I am an error!'; | ||
| }); | ||
|
|
||
| await page.goto('/'); | ||
| const pageloadSpan = await pageloadSpanPromise; | ||
|
|
||
| // Only capture error once the pageload span was sent | ||
| const exceptionButton = page.locator('id=exception-button'); | ||
| await exceptionButton.click(); | ||
|
|
||
| const errorEvent = await errorEventPromise; | ||
|
|
||
| expect(errorEvent.exception?.values).toHaveLength(1); | ||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('I am an error!'); | ||
|
|
||
| expect(errorEvent.transaction).toEqual('/'); | ||
|
|
||
| expect(errorEvent.contexts?.trace).toEqual({ | ||
| trace_id: pageloadSpan.trace_id, | ||
| span_id: expect.not.stringContaining(pageloadSpan.span_id), | ||
| }); | ||
| }); |
44 changes: 44 additions & 0 deletions
44
.../test-applications/react-router-6-router-entry/tests/navigation-trace-propagation.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { getSpanOp, waitForStreamedSpan } from '@sentry-internal/test-utils'; | ||
|
|
||
| test('propagates the navigation trace (not the stale pageload trace) for a fetch in a route mount effect', async ({ | ||
| page, | ||
| }) => { | ||
| // Intercept the /products data fetch and capture the tracing header the SDK attached. | ||
| let productsRequestSentryTrace: string | undefined; | ||
| await page.route('**/api/products', async route => { | ||
| productsRequestSentryTrace = route.request().headers()['sentry-trace']; | ||
| await route.fulfill({ | ||
| status: 200, | ||
| contentType: 'application/json', | ||
| body: '[]', | ||
| }); | ||
| }); | ||
|
|
||
| const pageloadSpanPromise = waitForStreamedSpan('react-router-6-router-entry', span => { | ||
| return getSpanOp(span) === 'pageload' && span.is_segment; | ||
| }); | ||
|
|
||
| const navigationSpanPromise = waitForStreamedSpan('react-router-6-router-entry', span => { | ||
| return getSpanOp(span) === 'navigation' && span.is_segment && span.name === '/products'; | ||
| }); | ||
|
|
||
| await page.goto('/'); | ||
| const pageloadSpan = await pageloadSpanPromise; | ||
|
|
||
| await page.locator('id=navigation-products').click(); | ||
| const navigationSpan = await navigationSpanPromise; | ||
|
|
||
| const pageloadTraceId = pageloadSpan.trace_id; | ||
| const navigationTraceId = navigationSpan.trace_id; | ||
| const propagatedTraceId = productsRequestSentryTrace?.split('-')[0]; | ||
|
|
||
| expect(pageloadTraceId).toBeDefined(); | ||
| expect(navigationTraceId).toBeDefined(); | ||
| expect(propagatedTraceId).toBeDefined(); | ||
| expect(navigationTraceId).not.toEqual(pageloadTraceId); | ||
|
|
||
| // The fetch fired on /products must carry the navigation trace, not the stale pageload trace. | ||
| expect(propagatedTraceId).toEqual(navigationTraceId); | ||
| expect(propagatedTraceId).not.toEqual(pageloadTraceId); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
react-routeris a dependency ofreact-router-dom(source), so people might not need to install it (it's installed anyway), but pnpm might be stricter with that.But
react-routerin v6 also mentions that nothing should be directly imported from there (but I guess thi only applies to users): https://github.com/remix-run/react-router/tree/v6/packages/react-routerFrom v7,
react-routeris the package to import from: https://reactrouter.com/7.18.3/upgrading/v6#upgrade-to-v7There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@s1gr1d added e2e tests for react router v6 and v8 as well. in v6 I on purpose did not add the react-router v6 dependency directly and it seems to work, though this may or may not work depending on package manager etc. I think it's fine to recommend people to add this dependency (does not hurt at least) but it should mostly also work without!