Skip to content

Commit e167c0c

Browse files
committed
update React Router Framework quick start guide
1 parent c5fd8dd commit e167c0c

File tree

2 files changed

+148
-46
lines changed

2 files changed

+148
-46
lines changed

docs/platforms/javascript/guides/react-router/index.mdx

Lines changed: 130 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: React Router Framework
3-
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.
44
sdk: sentry.javascript.react-router
55
categories:
66
- javascript
@@ -18,15 +18,15 @@ categories:
1818

1919
<Alert title='Looking for library mode?' level='info'>
2020

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).
2222

2323
</Alert>
2424

2525
<PlatformContent includePath="getting-started-prerequisites" />
2626

27-
## Install
27+
## Step 1: Install
2828

29-
Sentry captures data by using an SDK within your application's runtime.
29+
Choose the features you want to configure, and this guide will show you how:
3030

3131
<OnboardingOptionButtons
3232
options={[
@@ -42,6 +42,12 @@ Sentry captures data by using an SDK within your application's runtime.
4242
]}
4343
/>
4444

45+
<PlatformContent includePath="getting-started-features-expandable" />
46+
47+
### Install the Sentry SDK
48+
49+
Run the command for your preferred package manager to add the SDK package to your application:
50+
4551
<OnboardingOption optionId="profiling">
4652

4753
```bash {tabTitle:npm}
@@ -73,20 +79,19 @@ pnpm add @sentry/react-router
7379

7480
</OnboardingOption>
7581

76-
## Configure
82+
## Step 2: Configure
7783

78-
### Expose Hooks
84+
### Expose Entry Point Files
7985

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:
8287

8388
```bash
8489
npx react-router reveal
8590
```
8691

87-
### Client-Side Setup
92+
### Configure Client-side Sentry
8893

89-
Initialize the Sentry React SDK in your `entry.client.tsx` file:
94+
Initialize Sentry in your `entry.client.tsx` file:
9095

9196
```tsx {diff} {filename: entry.client.tsx}
9297
+import * as Sentry from "@sentry/react-router";
@@ -103,9 +108,14 @@ Initialize the Sentry React SDK in your `entry.client.tsx` file:
103108
+
104109
+ integrations: [
105110
+ // ___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
106114
+ Sentry.reactRouterTracingIntegration(),
107115
+ // ___PRODUCT_OPTION_END___ performance
108116
+ // ___PRODUCT_OPTION_START___ session-replay
117+
+ // Registers the Replay integration,
118+
+ // which automatically captures Session Replays
109119
+ Sentry.replayIntegration(),
110120
+ // ___PRODUCT_OPTION_END___ session-replay
111121
+ // ___PRODUCT_OPTION_START___ user-feedback
@@ -122,6 +132,11 @@ Initialize the Sentry React SDK in your `entry.client.tsx` file:
122132
+ // ___PRODUCT_OPTION_END___ logs
123133
+ // ___PRODUCT_OPTION_START___ performance
124134
+
135+
+ // Set tracesSampleRate to 1.0 to capture 100%
136+
+ // of transactions for tracing.
137+
+ // We recommend adjusting this value in production
138+
+ // Learn more at
139+
+ // https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#traces-sample-rate
125140
+ tracesSampleRate: 1.0, // Capture 100% of the transactions
126141
+
127142
+ // Set `tracePropagationTargets` to declare which URL(s) should have trace propagation enabled
@@ -131,6 +146,8 @@ Initialize the Sentry React SDK in your `entry.client.tsx` file:
131146
+
132147
+ // Capture Replay for 10% of all sessions,
133148
+ // plus 100% of sessions with an error
149+
+ // Learn more at
150+
+ // https://docs.sentry.io/platforms/javascript/guides/react-router/session-replay/configuration/#general-integration-configuration
134151
+ replaysSessionSampleRate: 0.1,
135152
+ replaysOnErrorSampleRate: 1.0,
136153
+ // ___PRODUCT_OPTION_END___ session-replay
@@ -146,7 +163,9 @@ startTransition(() => {
146163
});
147164
```
148165

149-
Now, update your `app/root.tsx` file to report any unhandled errors from your error boundary:
166+
#### Report Errors from Error Boundaries
167+
168+
Update your `app/root.tsx` file to capture unhandled errors in your error boundary:
150169

151170
```tsx {diff} {filename: app/root.tsx}
152171
+import * as Sentry from "@sentry/react-router";
@@ -186,14 +205,14 @@ export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
186205
// ...
187206
```
188207

189-
### Server-Side Setup
208+
### Configure Server-side Sentry
190209

191-
<Expandable level="warning" title="Limited Node support for auto instrumentation" permalink>
210+
<Expandable level="warning" title="Limited Node support for auto-instrumentation" permalink>
192211
Automatic server-side instrumentation is currently only supported on:
193212
- **Node 20:** Version \<20.19
194213
- **Node 22:** Version \<22.12
195214

196-
If you are on a different version please make use of our manual server wrappers.
215+
If you're on a different version, use our manual server wrappers.
197216

198217
For server loaders use `wrapServerLoader`:
199218

@@ -229,7 +248,7 @@ export const action = Sentry.wrapServerAction(
229248

230249
</Expandable>
231250

232-
Create an `instrument.server.mjs` file in the root of your app:
251+
First, create a file called `instrument.server.mjs` in the root of your project to initialize Sentry:
233252

234253
```js {filename: instrument.server.mjs}
235254
import * as Sentry from "@sentry/react-router";
@@ -250,18 +269,29 @@ Sentry.init({
250269
// ___PRODUCT_OPTION_END___ logs
251270
// ___PRODUCT_OPTION_START___ profiling
252271

272+
// Add our Profiling integration
253273
integrations: [nodeProfilingIntegration()],
254274
// ___PRODUCT_OPTION_END___ profiling
255275
// ___PRODUCT_OPTION_START___ performance
256-
tracesSampleRate: 1.0, // Capture 100% of the transactions
276+
// Set tracesSampleRate to 1.0 to capture 100%
277+
// of transactions for tracing.
278+
// We recommend adjusting this value in production
279+
// Learn more at
280+
// https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#tracesSampleRate
281+
tracesSampleRate: 1.0,
257282
// ___PRODUCT_OPTION_END___ performance
258283
// ___PRODUCT_OPTION_START___ profiling
259-
profilesSampleRate: 1.0, // profile every transaction
284+
// Set profilesSampleRate to 1.0 to profile 100%
285+
// of sampled transactions.
286+
// This is relative to tracesSampleRate
287+
// Learn more at
288+
// https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#profilesSampleRate
289+
profilesSampleRate: 1.0,
260290
// ___PRODUCT_OPTION_END___ profiling
261291
});
262292
```
263293

264-
Update your `entry.server.tsx` file:
294+
Next, replace the default `handleRequest` and `handleError` functions in your `entry.server.tsx` file with Sentry's wrapped versions:
265295

266296
```tsx {diff} {filename: entry.server.tsx}
267297
+import * as Sentry from '@sentry/react-router';
@@ -286,7 +316,7 @@ Update your `entry.server.tsx` file:
286316
```
287317

288318
<Expandable title="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:
290320

291321
```tsx {1-4, 44-45, 69-70}
292322
import {
@@ -375,11 +405,7 @@ import {
375405

376406
export function handleError(
377407
error: unknown,
378-
{
379-
request,
380-
params,
381-
context,
382-
}: LoaderFunctionArgs | ActionFunctionArgs
408+
{ request, params, context }: LoaderFunctionArgs | ActionFunctionArgs
383409
) {
384410
if (!request.signal.aborted) {
385411
Sentry.captureException(error);
@@ -392,23 +418,22 @@ export function handleError(
392418

393419
</Expandable>
394420

395-
### Update Scripts
421+
#### Load Instrumentation on Startup
396422

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:
399-
400-
<PlatformContent includePath="node-options-windows" />
423+
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:
401424

402425
```json {filename: package.json}
403426
"scripts": {
404-
"dev": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router dev",
405-
"start": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router-serve ./build/server/index.js",
427+
"dev": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router dev",
428+
"start": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router-serve ./build/server/index.js",
406429
}
407430
```
408431

409-
#### Alternative Setup for Hosting Platforms
432+
<PlatformContent includePath="node-options-windows" />
433+
434+
**Deploying to Vercel, Netlify, and similar platforms**
410435

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`:
412437

413438
```tsx {diff} {filename: entry.server.tsx}
414439
+import './instrument.server';
@@ -420,13 +445,15 @@ If you're deploying to platforms like **Vercel** or **Netlify** where setting th
420445

421446
<Alert title="Incomplete Auto-instrumentation" level="warning">
422447

423-
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.
424449

425450
</Alert>
426451

427-
## Source Maps Upload
452+
## Step 3: Add Readable Stack Traces With Source Maps (Optional)
428453

429-
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:
430457

431458
```typescript {filename: vite.config.ts} {diff}
432459
import { reactRouter } from '@react-router/dev/vite';
@@ -458,7 +485,7 @@ To keep your auth token secure, always store it in an environment variable inste
458485
SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
459486
```
460487

461-
Include the `sentryOnBuildEnd` hook in `react-router.config.ts`:
488+
Next, include the `sentryOnBuildEnd` hook in `react-router.config.ts`:
462489

463490
```typescript {filename: react-router.config.ts} {diff}
464491
import type { Config } from "@react-router/dev/config";
@@ -474,29 +501,86 @@ export default {
474501
} satisfies Config;
475502
```
476503

477-
## Verify
504+
## Step 4: Avoid Ad Blockers With Tunneling (Optional)
505+
506+
<PlatformContent includePath="getting-started-tunneling" />
478507

479-
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
480509

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:
483515

484516
```tsx {filename: error.tsx}
485517
import type { Route } from "./+types/example-page";
486518

487519
export async function loader() {
488-
throw new Error("some error thrown in a loader");
520+
throw new Error("My first Sentry error!");
489521
}
490522

491523
export default function ExamplePage() {
492524
return <div>Loading this page will throw an error</div>;
493525
}
494526
```
495527

496-
<Alert>
528+
<OnboardingOption optionId="performance" hideForThisOption>
529+
Open the route in your browser and you should trigger two errors: one from the
530+
server-side and one from the client-side.
531+
</OnboardingOption>
532+
533+
<PlatformContent includePath="getting-started-browser-sandbox-warning" />
497534

498-
Learn more about manually capturing an error or message in our <PlatformLink to="/usage/">Usage documentation</PlatformLink>.
535+
<OnboardingOption optionId="performance">
536+
### Tracing
537+
To test your tracing configuration, update the previous code snippet by starting a trace to measure the time it takes for the execution of your code:
499538

500-
</Alert>
539+
```tsx {filename: error.tsx}
540+
import * as Sentry from "@sentry/react-router";
541+
import type { Route } from "./+types/example-page";
542+
543+
export async function loader() {
544+
return Sentry.startSpan(
545+
{
546+
op: "test",
547+
name: "My First Test Transaction",
548+
},
549+
() => {
550+
throw new Error("My first Sentry error!");
551+
}
552+
);
553+
}
554+
555+
export default function ExamplePage() {
556+
return <div>Loading this page will throw an error</div>;
557+
}
558+
```
559+
560+
Open the route in your browser. You should start a trace and trigger two errors: one from the server side and one from the client side.
561+
562+
</OnboardingOption>
563+
564+
### View Captured Data in Sentry
565+
566+
Now, head over to your project on [Sentry.io](https://sentry.io) to view the collected data (it takes a couple of moments for the data to appear).
567+
568+
<PlatformContent includePath="getting-started-verify-locate-data" />
569+
570+
## Next Steps
571+
572+
At this point, you should have integrated Sentry into your React Router Framework application and should already be sending data to your Sentry project.
501573

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 <PlatformLink to="/usage">manually capture errors</PlatformLink>
578+
- Continue to <PlatformLink to="/configuration">customize your configuration</PlatformLink>
579+
- Get familiar with [Sentry's product features](/product/) like tracing, insights, and alerts
580+
581+
<Expandable permalink={false} title="Are you having problems setting up the SDK?">
582+
583+
- Find various topics in <PlatformLink to="/troubleshooting">Troubleshooting</PlatformLink>
584+
- [Get support](https://sentry.zendesk.com/hc/en-us/)
585+
586+
</Expandable>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Expandable title="Want to learn more about these features?">
2+
3+
- [**Issues**](/product/issues) (always enabled): Sentry's core error monitoring product that automatically reports errors,
4+
uncaught exceptions, and unhandled rejections. If you have something that
5+
looks like an exception, Sentry can capture it.
6+
- [**Tracing**](/product/tracing): Track software performance while seeing the
7+
impact of errors across multiple systems. For example, distributed tracing
8+
allows you to follow a request from the frontend to the backend and back.
9+
- [**Session Replay**](/product/explore/session-replay/web):
10+
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.
17+
18+
</Expandable>

0 commit comments

Comments
 (0)