You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: React Router v7 is a framework for building full-stack web apps and websites. Learn how to set it up with Sentry.
3
+
description: Learn how to set up and configure Sentry in your React Router v7 application, capture your first errors, and view them in Sentry.
4
4
sdk: sentry.javascript.react-router
5
5
categories:
6
6
- javascript
@@ -18,15 +18,15 @@ categories:
18
18
19
19
<Alerttitle='Looking for library mode?'level='info'>
20
20
21
-
If you are using React Router in library mode, you can follow the instructions in the[React guide here](/platforms/javascript/guides/react/features/react-router/v7).
21
+
If you're using React Router in library mode, follow the instructions in our[React guide](/platforms/javascript/guides/react/features/react-router/v7).
Run the command for your preferred package manager to add the SDK package to your application:
50
+
45
51
<OnboardingOptionoptionId="profiling">
46
52
47
53
```bash {tabTitle:npm}
@@ -73,20 +79,19 @@ pnpm add @sentry/react-router
73
79
74
80
</OnboardingOption>
75
81
76
-
## Configure
82
+
## Step 2: Configure
77
83
78
-
### Expose Hooks
84
+
### Expose Entry Point Files
79
85
80
-
React Router exposes two hooks in your `app` folder (`entry.client.tsx` and `entry.server.tsx`).
81
-
If you do not see these two files, expose them with the following command:
86
+
Before configuring Sentry, you need to make React Router's entry files (`entry.client.tsx` and `entry.server.tsx`) visible in your project. Run this command to expose them:
82
87
83
88
```bash
84
89
npx react-router reveal
85
90
```
86
91
87
-
### Client-Side Setup
92
+
### Configure Client-side Sentry
88
93
89
-
Initialize the Sentry React SDK in your `entry.client.tsx` file:
94
+
Initialize Sentry in your `entry.client.tsx` file:
90
95
91
96
```tsx {diff} {filename: entry.client.tsx}
92
97
+import*asSentryfrom"@sentry/react-router";
@@ -103,9 +108,14 @@ Initialize the Sentry React SDK in your `entry.client.tsx` file:
103
108
+
104
109
+ integrations: [
105
110
+// ___PRODUCT_OPTION_START___ performance
111
+
+// Registers and configures the Tracing integration,
112
+
+// which automatically instruments your application to monitor its
113
+
+// performance, including custom Angular routing instrumentation
106
114
+Sentry.reactRouterTracingIntegration(),
107
115
+// ___PRODUCT_OPTION_END___ performance
108
116
+// ___PRODUCT_OPTION_START___ session-replay
117
+
+// Registers the Replay integration,
118
+
+// which automatically captures Session Replays
109
119
+Sentry.replayIntegration(),
110
120
+// ___PRODUCT_OPTION_END___ session-replay
111
121
+// ___PRODUCT_OPTION_START___ user-feedback
@@ -122,6 +132,11 @@ Initialize the Sentry React SDK in your `entry.client.tsx` file:
122
132
+ // ___PRODUCT_OPTION_END___ logs
123
133
+ // ___PRODUCT_OPTION_START___ performance
124
134
+
135
+
+ // Set tracesSampleRate to 1.0 to capture 100%
136
+
+ // of transactions for tracing.
137
+
+ // We recommend adjusting this value in production
Next, replace the default `handleRequest` and `handleError` functions in your `entry.server.tsx` file with Sentry's wrapped versions:
265
295
266
296
```tsx {diff} {filename: entry.server.tsx}
267
297
+import*asSentryfrom'@sentry/react-router';
@@ -286,7 +316,7 @@ Update your `entry.server.tsx` file:
286
316
```
287
317
288
318
<Expandabletitle="Do you need to customize your handleRequest function?">
289
-
If you need to update the logic of your `handleRequest` function you'll need to include the provided Sentry helper functions (`getMetaTagTransformer` and `wrapSentryHandleRequest`) manually:
319
+
If you need to customize the logic of your `handleRequest` function, you'll need to use Sentry's helper functions (`getMetaTagTransformer` and `wrapSentryHandleRequest`) manually:
@@ -392,23 +418,22 @@ export function handleError(
392
418
393
419
</Expandable>
394
420
395
-
###Update Scripts
421
+
#### Load Instrumentation on Startup
396
422
397
-
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.
398
-
Update the `start` and `dev` script to include the instrumentation file:
React Router runs in ESM mode, which means you need to load the Sentry instrumentation file before the application starts. Update your `package.json` scripts:
**Deploying to Vercel, Netlify, and similar platforms**
410
435
411
-
If you're deploying to platforms like **Vercel** or **Netlify** where setting the `NODE_OPTIONS`import flag is not possible, you can import the instrumentation file directly at the top of your `entry.server.tsx`:
436
+
If you're deploying to platforms where you can't set the `NODE_OPTIONS` flag, import the instrumentation file directly at the top of your `entry.server.tsx`:
412
437
413
438
```tsx {diff} {filename: entry.server.tsx}
414
439
+import'./instrument.server';
@@ -420,13 +445,15 @@ If you're deploying to platforms like **Vercel** or **Netlify** where setting th
When importing the instrumentation file directly in `entry.server.tsx`instead of using the `--import` flag, automatic instrumentation will be incomplete and you'll miss automatically captured spans and traces for some of your application's server-side operations. This approach should only be used when the `NODE_OPTIONS`approach is not available on your hosting platform.
448
+
When you import the instrumentation file directly instead of using the `--import` flag, automatic instrumentation will be incomplete. You'll miss automatically captured spans and traces for some server-side operations. Only use this approach when the `NODE_OPTIONS`method isn't available.
Update `vite.config.ts` to include the `sentryReactRouter` plugin, making sure to pass both the Vite and Sentry configurations to it:
454
+
The stack traces in your Sentry errors probably won't look like your actual code without unminifying them. To fix this, upload your source maps to Sentry.
455
+
456
+
First, update `vite.config.ts` to include the `sentryReactRouter` plugin, making sure to pass both the Vite and Sentry configurations to it:
This snippet includes an intentional error, so you can test that everything is working as soon as you set it up.
508
+
## Step 5: Verify Your Setup
480
509
481
-
Throw an error in a loader to verify that Sentry is working.
482
-
After opening this route in your browser, you should see two errors in the Sentry issue stream, one captured from the server and one captured from the client.
510
+
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
511
+
512
+
### Issues
513
+
514
+
To verify that Sentry captures errors and creates issues in your Sentry project, throw an error in a loader:
483
515
484
516
```tsx {filename: error.tsx}
485
517
importtype { Route } from"./+types/example-page";
486
518
487
519
exportasyncfunction loader() {
488
-
thrownewError("some error thrown in a loader");
520
+
thrownewError("My first Sentry error!");
489
521
}
490
522
491
523
exportdefaultfunction ExamplePage() {
492
524
return <div>Loading this page will throw an error</div>;
At this point, you should have integrated Sentry into your React Router Framework application and should already be sending data to your Sentry project.
501
573
502
-
To view and resolve the recorded error, log into [sentry.io](https://sentry.io) and select your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.
574
+
Now's a good time to customize your setup and look into more advanced topics.
575
+
Our next recommended steps for you are:
576
+
577
+
- Learn how to <PlatformLinkto="/usage">manually capture errors</PlatformLink>
578
+
- Continue to <PlatformLinkto="/configuration">customize your configuration</PlatformLink>
579
+
- Get familiar with [Sentry's product features](/product/) like tracing, insights, and alerts
580
+
581
+
<Expandablepermalink={false}title="Are you having problems setting up the SDK?">
582
+
583
+
- Find various topics in <PlatformLinkto="/troubleshooting">Troubleshooting</PlatformLink>
Get to the root cause of an issue faster by viewing a video-like reproduction
11
+
of what was happening in the user's browser before, during, and after the
12
+
problem.
13
+
-[**Profiling**](/product/explore/profiling/): Gain deeper insight than traditional tracing without custom instrumentation, letting you discover slow-to-execute or resource-intensive functions in your app.
14
+
-[**Logs**](/product/explore/logs): Centralize and analyze your application logs to
15
+
correlate them with errors and performance issues. Search, filter, and
16
+
visualize log data to understand what's happening in your applications.
0 commit comments