|
| 1 | +<p align="center"> |
| 2 | + <a href="https://sentry.io/?utm_source=github&utm_medium=logo" target="_blank"> |
| 3 | + <img src="https://sentry-brand.storage.googleapis.com/sentry-wordmark-dark-280x84.png" alt="Sentry" width="280" height="84"> |
| 4 | + </a> |
| 5 | +</p> |
| 6 | + |
| 7 | +# Official Sentry SDK for React Router (Framework) (EXPERIMENTAL) |
| 8 | + |
| 9 | +[](https://www.npmjs.com/package/@sentry/react-router) |
| 10 | +[](https://www.npmjs.com/package/@sentry/react-router) |
| 11 | +[](https://www.npmjs.com/package/@sentry/react-router) |
| 12 | + |
| 13 | +> [!WARNING] |
| 14 | +> This SDK is considered ⚠️ **experimental and in an alpha state**. It may experience breaking changes. Please reach out |
| 15 | +> on [GitHub](https://github.com/getsentry/sentry-javascript/issues/) if you have any feedback or concerns. This |
| 16 | +> SDK is for [React Router (framework)](https://reactrouter.com/start/framework/installation). If you're using [React Router (library)](https://reactrouter.com/start/library/installation) see our |
| 17 | +> [React SDK here](https://docs.sentry.io/platforms/javascript/guides/react/features/react-router/v7/). |
| 18 | +
|
| 19 | +## Links |
| 20 | + |
| 21 | +- [Official SDK Docs](https://docs.sentry.io/platforms/javascript/guides/react-router/) |
| 22 | + |
| 23 | +## General |
| 24 | + |
| 25 | +This package is a wrapper around `@sentry/node` for the server and `@sentry/browser` for the client side. |
| 26 | + |
| 27 | +## Manual Setup |
| 28 | + |
| 29 | +### Expose Hooks |
| 30 | + |
| 31 | +React Router exposes two hooks in your `app` folder (`entry.client.tsx` and `entry.server.tsx`). |
| 32 | +If you do not see these two files, expose them with the following command: |
| 33 | + |
| 34 | +```bash |
| 35 | +npx react-router reveal |
| 36 | +``` |
| 37 | + |
| 38 | +### Client-Side Setup |
| 39 | + |
| 40 | +Initialize the SDK in your `entry.client.tsx` file: |
| 41 | + |
| 42 | +```tsx |
| 43 | +import * as Sentry from '@sentry/react-router'; |
| 44 | +import { startTransition, StrictMode } from 'react'; |
| 45 | +import { hydrateRoot } from 'react-dom/client'; |
| 46 | +import { HydratedRouter } from 'react-router/dom'; |
| 47 | + |
| 48 | +Sentry.init({ |
| 49 | + dsn: '___PUBLIC_DSN___', |
| 50 | + integrations: [Sentry.browserTracingIntegration()], |
| 51 | + |
| 52 | + tracesSampleRate: 1.0, // Capture 100% of the transactions |
| 53 | + |
| 54 | + // Set `tracePropagationTargets` to declare which URL(s) should have trace propagation enabled |
| 55 | + tracePropagationTargets: [/^\//, /^https:\/\/yourserver\.io\/api/], |
| 56 | +}); |
| 57 | + |
| 58 | +startTransition(() => { |
| 59 | + hydrateRoot( |
| 60 | + document, |
| 61 | + <StrictMode> |
| 62 | + <HydratedRouter /> |
| 63 | + </StrictMode>, |
| 64 | + ); |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +Now, update your `app/root.tsx` file to report any unhandled errors from your global error boundary: |
| 69 | + |
| 70 | +```tsx |
| 71 | +import * as Sentry from '@sentry/react-router'; |
| 72 | + |
| 73 | +export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) { |
| 74 | + let message = 'Oops!'; |
| 75 | + let details = 'An unexpected error occurred.'; |
| 76 | + let stack: string | undefined; |
| 77 | + |
| 78 | + if (isRouteErrorResponse(error)) { |
| 79 | + message = error.status === 404 ? '404' : 'Error'; |
| 80 | + details = error.status === 404 ? 'The requested page could not be found.' : error.statusText || details; |
| 81 | + } else if (error && error instanceof Error) { |
| 82 | + // you only want to capture non 404-errors that reach the boundary |
| 83 | + Sentry.captureException(error); |
| 84 | + if (import.meta.env.DEV) { |
| 85 | + details = error.message; |
| 86 | + stack = error.stack; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return ( |
| 91 | + <main> |
| 92 | + <h1>{message}</h1> |
| 93 | + <p>{details}</p> |
| 94 | + {stack && ( |
| 95 | + <pre> |
| 96 | + <code>{stack}</code> |
| 97 | + </pre> |
| 98 | + )} |
| 99 | + </main> |
| 100 | + ); |
| 101 | +} |
| 102 | +// ... |
| 103 | +``` |
| 104 | + |
| 105 | +### Server-Side Setup |
| 106 | + |
| 107 | +Create an `instrument.server.mjs` file in the root of your app: |
| 108 | + |
| 109 | +```js |
| 110 | +import * as Sentry from '@sentry/node'; |
| 111 | + |
| 112 | +Sentry.init({ |
| 113 | + dsn: '___PUBLIC_DSN___', |
| 114 | + tracesSampleRate: 1.0, // Capture 100% of the transactions |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +In your `entry.server.tsx` file, export the `handleError` function: |
| 119 | + |
| 120 | +```tsx |
| 121 | +import * as Sentry from '@sentry/node'; |
| 122 | +import { type HandleErrorFunction } from 'react-router'; |
| 123 | + |
| 124 | +export const handleError: HandleErrorFunction = (error, { request }) => { |
| 125 | + // React Router may abort some interrupted requests, report those |
| 126 | + if (!request.signal.aborted) { |
| 127 | + Sentry.captureException(error); |
| 128 | + |
| 129 | + // make sure to still log the error so you can see it |
| 130 | + console.error(error); |
| 131 | + } |
| 132 | +}; |
| 133 | +// ... rest of your server entry |
| 134 | +``` |
| 135 | + |
| 136 | +### Update Scripts |
| 137 | + |
| 138 | +Since React Router is running in ESM mode, you need to use the `--import` command line options to load our server-side instrumentation module before the application starts. |
| 139 | +Update the `start` and `dev` script to include the instrumentation file: |
| 140 | + |
| 141 | +```json |
| 142 | +"scripts": { |
| 143 | + "dev": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router dev", |
| 144 | + "start": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router-serve ./build/server/index.js", |
| 145 | +} |
| 146 | +``` |
0 commit comments