Skip to content

Commit ff9dec7

Browse files
committed
update readme
1 parent 7af5db6 commit ff9dec7

File tree

1 file changed

+126
-4
lines changed

1 file changed

+126
-4
lines changed

packages/react-router/README.md

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
[![npm dm](https://img.shields.io/npm/dm/@sentry/react-router.svg)](https://www.npmjs.com/package/@sentry/react-router)
1111
[![npm dt](https://img.shields.io/npm/dt/@sentry/react-router.svg)](https://www.npmjs.com/package/@sentry/react-router)
1212

13-
This SDK is considered ⚠️ **experimental and in an alpha state**. It may experience breaking changes. Please reach out
14-
on [GitHub](https://github.com/getsentry/sentry-javascript/issues/) if you have any feedback or concerns. This
15-
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
16-
[React SDK here](https://docs.sentry.io/platforms/javascript/guides/react/features/react-router/v7/).
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/).
1718
1819
## Links
1920

@@ -22,3 +23,124 @@ SDK is for [React Router (framework)](https://reactrouter.com/start/framework/in
2223
## General
2324

2425
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 {diff} {filename: app/root.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 {filename: instrument.server.mjs} {"onboardingOptions": {"performance": "7", "profiling": "2, 6, 8"}}
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 {diff} {filename: entry.server.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 {filename: package.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

Comments
 (0)