Manual Setup
Learn how to manually set up Sentry in your React Router v7 app and capture your first errors.
Important
This SDK is currently in beta. Beta features are still in progress and may have bugs. Please reach out on GitHub if you have any feedback or concerns.
Looking for automatic setup with sentry init or the React Router Framework Mode wizard? Follow the React Router quickstart instead. Continue with this guide to set up Sentry manually.
You need:
Choose the features you want to configure, and this guide will show you how:
Run the command for your preferred package manager to add the SDK package to your application:
npm install @sentry/react-router @sentry/profiling-node
npm install @sentry/react-router @sentry/profiling-node
yarn add @sentry/react-router @sentry/profiling-node
pnpm add @sentry/react-router @sentry/profiling-node
npm install @sentry/react-router
npm install @sentry/react-router
yarn add @sentry/react-router
pnpm add @sentry/react-router
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:
npx react-router reveal
npx react-router reveal
Initialize Sentry in your entry.client.tsx file.
The sentryOnError handler integrates with React Router's onError hook to automatically capture and report client-side errors to Sentry.
entry.client.tsx+import * as Sentry from "@sentry/react-router";
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import { HydratedRouter } from "react-router/dom";
+Sentry.init({
+ dsn: "___PUBLIC_DSN___",
+
+ // To disable sending user data, uncomment the line below. For more info visit:
+ // https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#dataCollection
+ // dataCollection: { userInfo: false },
+
+ integrations: [
+ // ___PRODUCT_OPTION_START___ performance
+ // Registers and configures the Tracing integration,
+ // which automatically instruments your application to monitor its
+ // performance, including custom React Router routing instrumentation
+ Sentry.reactRouterTracingIntegration(),
+ // ___PRODUCT_OPTION_END___ performance
+ // ___PRODUCT_OPTION_START___ session-replay
+ // Registers the Replay integration,
+ // which automatically captures Session Replays
+ Sentry.replayIntegration(),
+ // ___PRODUCT_OPTION_END___ session-replay
+ // ___PRODUCT_OPTION_START___ user-feedback
+ Sentry.feedbackIntegration({
+ // Additional SDK configuration goes in here, for example:
+ colorScheme: "system",
+ }),
+ // ___PRODUCT_OPTION_END___ user-feedback
+ ],
+ // ___PRODUCT_OPTION_START___ logs
+
+ // Enable logs to be sent to Sentry
+ enableLogs: true,
+ // ___PRODUCT_OPTION_END___ logs
+ // ___PRODUCT_OPTION_START___ performance
+
+ // Set tracesSampleRate to 1.0 to capture 100%
+ // of transactions for tracing.
+ // We recommend adjusting this value in production
+ // Learn more at
+ // https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#traces-sample-rate
+ tracesSampleRate: 1.0,
+
+ // Set `tracePropagationTargets` to declare which URL(s) should have trace propagation enabled
+ tracePropagationTargets: [/^\//, /^https:\/\/yourserver\.io\/api/],
+ // ___PRODUCT_OPTION_END___ performance
+ // ___PRODUCT_OPTION_START___ session-replay
+
+ // Capture Replay for 10% of all sessions,
+ // plus 100% of sessions with an error
+ // Learn more at
+ // https://docs.sentry.io/platforms/javascript/guides/react-router/session-replay/configuration/#general-integration-configuration
+ replaysSessionSampleRate: 0.1,
+ replaysOnErrorSampleRate: 1.0,
+ // ___PRODUCT_OPTION_END___ session-replay
+});
startTransition(() => {
hydrateRoot(
document,
<StrictMode>
<HydratedRouter onError={Sentry.sentryOnError} />
</StrictMode>
);
});
+import * as Sentry from "@sentry/react-router";
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import { HydratedRouter } from "react-router/dom";
+Sentry.init({
+ dsn: "___PUBLIC_DSN___",
+
+ // To disable sending user data, uncomment the line below. For more info visit:
+ // https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#dataCollection
+ // dataCollection: { userInfo: false },
+
+ integrations: [
+ // ___PRODUCT_OPTION_START___ performance
+ // Registers and configures the Tracing integration,
+ // which automatically instruments your application to monitor its
+ // performance, including custom React Router routing instrumentation
+ Sentry.reactRouterTracingIntegration(),
+ // ___PRODUCT_OPTION_END___ performance
+ // ___PRODUCT_OPTION_START___ session-replay
+ // Registers the Replay integration,
+ // which automatically captures Session Replays
+ Sentry.replayIntegration(),
+ // ___PRODUCT_OPTION_END___ session-replay
+ // ___PRODUCT_OPTION_START___ user-feedback
+ Sentry.feedbackIntegration({
+ // Additional SDK configuration goes in here, for example:
+ colorScheme: "system",
+ }),
+ // ___PRODUCT_OPTION_END___ user-feedback
+ ],
+ // ___PRODUCT_OPTION_START___ logs
+
+ // Enable logs to be sent to Sentry
+ enableLogs: true,
+ // ___PRODUCT_OPTION_END___ logs
+ // ___PRODUCT_OPTION_START___ performance
+
+ // Set tracesSampleRate to 1.0 to capture 100%
+ // of transactions for tracing.
+ // We recommend adjusting this value in production
+ // Learn more at
+ // https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#traces-sample-rate
+ tracesSampleRate: 1.0,
+
+ // Set `tracePropagationTargets` to declare which URL(s) should have trace propagation enabled
+ tracePropagationTargets: [/^\//, /^https:\/\/yourserver\.io\/api/],
+ // ___PRODUCT_OPTION_END___ performance
+ // ___PRODUCT_OPTION_START___ session-replay
+
+ // Capture Replay for 10% of all sessions,
+ // plus 100% of sessions with an error
+ // Learn more at
+ // https://docs.sentry.io/platforms/javascript/guides/react-router/session-replay/configuration/#general-integration-configuration
+ replaysSessionSampleRate: 0.1,
+ replaysOnErrorSampleRate: 1.0,
+ // ___PRODUCT_OPTION_END___ session-replay
+});
startTransition(() => {
hydrateRoot(
document,
<StrictMode>
<HydratedRouter onError={Sentry.sentryOnError} />
</StrictMode>
);
});
First, create a file called instrument.server.mjs in the root of your project to initialize Sentry:
instrument.server.mjsimport * as Sentry from "@sentry/react-router";
// ___PRODUCT_OPTION_START___ profiling
import { nodeProfilingIntegration } from "@sentry/profiling-node";
// ___PRODUCT_OPTION_END___ profiling
Sentry.init({
dsn: "___PUBLIC_DSN___",
// To disable sending user data, uncomment the line below. For more info visit:
// https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#dataCollection
// dataCollection: { userInfo: false },
// ___PRODUCT_OPTION_START___ logs
// Enable logs to be sent to Sentry
enableLogs: true,
// ___PRODUCT_OPTION_END___ logs
// ___PRODUCT_OPTION_START___ profiling
// Add our Profiling integration
integrations: [nodeProfilingIntegration()],
// ___PRODUCT_OPTION_END___ profiling
// ___PRODUCT_OPTION_START___ performance
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ performance
// ___PRODUCT_OPTION_START___ profiling
// Enable profiling for a percentage of sessions
// Learn more at
// https://docs.sentry.io/platforms/javascript/configuration/options/#profileSessionSampleRate
profileSessionSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ profiling
});
import * as Sentry from "@sentry/react-router";
// ___PRODUCT_OPTION_START___ profiling
import { nodeProfilingIntegration } from "@sentry/profiling-node";
// ___PRODUCT_OPTION_END___ profiling
Sentry.init({
dsn: "___PUBLIC_DSN___",
// To disable sending user data, uncomment the line below. For more info visit:
// https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#dataCollection
// dataCollection: { userInfo: false },
// ___PRODUCT_OPTION_START___ logs
// Enable logs to be sent to Sentry
enableLogs: true,
// ___PRODUCT_OPTION_END___ logs
// ___PRODUCT_OPTION_START___ profiling
// Add our Profiling integration
integrations: [nodeProfilingIntegration()],
// ___PRODUCT_OPTION_END___ profiling
// ___PRODUCT_OPTION_START___ performance
// Set tracesSampleRate to 1.0 to capture 100%
// of transactions for tracing.
// We recommend adjusting this value in production
// Learn more at
// https://docs.sentry.io/platforms/javascript/guides/react-router/configuration/options/#tracesSampleRate
tracesSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ performance
// ___PRODUCT_OPTION_START___ profiling
// Enable profiling for a percentage of sessions
// Learn more at
// https://docs.sentry.io/platforms/javascript/configuration/options/#profileSessionSampleRate
profileSessionSampleRate: 1.0,
// ___PRODUCT_OPTION_END___ profiling
});
Next, replace the default handleRequest and handleError functions in your entry.server.tsx file with Sentry's wrapped versions:
entry.server.tsx+import * as Sentry from '@sentry/react-router';
import { createReadableStreamFromReadable } from '@react-router/node';
import { renderToPipeableStream } from 'react-dom/server';
import { ServerRouter } from 'react-router';
import { type HandleErrorFunction } from 'react-router';
+const handleRequest = Sentry.createSentryHandleRequest({
+ ServerRouter,
+ renderToPipeableStream,
+ createReadableStreamFromReadable,
+});
export default handleRequest;
+export const handleError = Sentry.createSentryHandleError({
+ logErrors: false
+});
// ... rest of your server entry
+import * as Sentry from '@sentry/react-router';
import { createReadableStreamFromReadable } from '@react-router/node';
import { renderToPipeableStream } from 'react-dom/server';
import { ServerRouter } from 'react-router';
import { type HandleErrorFunction } from 'react-router';
+const handleRequest = Sentry.createSentryHandleRequest({
+ ServerRouter,
+ renderToPipeableStream,
+ createReadableStreamFromReadable,
+});
export default handleRequest;
+export const handleError = Sentry.createSentryHandleError({
+ logErrors: false
+});
// ... rest of your server entry
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:
package.json"scripts": {
"dev": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router dev",
"start": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router-serve ./build/server/index.js",
}
"scripts": {
"dev": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router dev",
"start": "NODE_OPTIONS='--import ./instrument.server.mjs' react-router-serve ./build/server/index.js",
}
Deploying to Vercel, Netlify, and similar platforms
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:
entry.server.tsx+import './instrument.server';
import * as Sentry from '@sentry/react-router';
import { createReadableStreamFromReadable } from '@react-router/node';
import { renderToPipeableStream } from 'react-dom/server';
// ... rest of your imports
+import './instrument.server';
import * as Sentry from '@sentry/react-router';
import { createReadableStreamFromReadable } from '@react-router/node';
import { renderToPipeableStream } from 'react-dom/server';
// ... rest of your imports
Incomplete Auto-instrumentation
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.
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.
First, update vite.config.ts to include the sentryReactRouter plugin, making sure to pass both the Vite and Sentry configurations to it:
vite.config.tsimport { reactRouter } from '@react-router/dev/vite';
import { sentryReactRouter, type SentryReactRouterBuildOptions } from '@sentry/react-router';
import { defineConfig } from 'vite';
const sentryConfig: SentryReactRouterBuildOptions = {
org: "___ORG_SLUG___",
project: "___PROJECT_SLUG___",
// An auth token is required for uploading source maps;
// store it in an environment variable to keep it secure.
authToken: process.env.SENTRY_AUTH_TOKEN,
// ...
};
export default defineConfig(config => {
return {
+ plugins: [reactRouter(),sentryReactRouter(sentryConfig, config)],
};
});
import { reactRouter } from '@react-router/dev/vite';
import { sentryReactRouter, type SentryReactRouterBuildOptions } from '@sentry/react-router';
import { defineConfig } from 'vite';
const sentryConfig: SentryReactRouterBuildOptions = {
org: "___ORG_SLUG___",
project: "___PROJECT_SLUG___",
// An auth token is required for uploading source maps;
// store it in an environment variable to keep it secure.
authToken: process.env.SENTRY_AUTH_TOKEN,
// ...
};
export default defineConfig(config => {
return {
+ plugins: [reactRouter(),sentryReactRouter(sentryConfig, config)],
};
});
To keep your auth token secure, always store it in an environment variable instead of directly in your files:
.envSENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
Next, include the sentryOnBuildEnd hook in react-router.config.ts:
react-router.config.tsimport type { Config } from "@react-router/dev/config";
import { sentryOnBuildEnd } from "@sentry/react-router";
export default {
ssr: true,
buildEnd: async ({ viteConfig, reactRouterConfig, buildManifest }) => {
// ...
// Call this at the end of the hook
+(await sentryOnBuildEnd({ viteConfig, reactRouterConfig, buildManifest }));
},
} satisfies Config;
import type { Config } from "@react-router/dev/config";
import { sentryOnBuildEnd } from "@sentry/react-router";
export default {
ssr: true,
buildEnd: async ({ viteConfig, reactRouterConfig, buildManifest }) => {
// ...
// Call this at the end of the hook
+(await sentryOnBuildEnd({ viteConfig, reactRouterConfig, buildManifest }));
},
} satisfies Config;
You can prevent ad blockers from blocking Sentry events using tunneling. Use the tunnel option in Sentry.init to add an API endpoint in your application that forwards Sentry events to Sentry servers.
This will send all events to the tunnel endpoint. However, the events need to be parsed and redirected to Sentry, so you'll need to do additional configuration on the server. You can find a detailed explanation on how to do this on our Troubleshooting page.
Sentry.init({
dsn: "___PUBLIC_DSN___",
tunnel: "/tunnel",
});
Sentry.init({
dsn: "___PUBLIC_DSN___",
tunnel: "/tunnel",
});
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
To verify that Sentry captures errors and creates issues in your Sentry project, throw an error in a loader.
Then, open the route in your browser and you should trigger an error.
Important
Errors triggered from within your browser's developer tools (like the browser console) are sandboxed, so they will not trigger Sentry's error monitoring.
error.tsximport type { Route } from "./+types/example-page";
export async function loader() {
throw new Error("My first Sentry error!");
}
export default function ExamplePage() {
return <div>Loading this page will throw an error</div>;
}
import type { Route } from "./+types/example-page";
export async function loader() {
throw new Error("My first Sentry error!");
}
export default function ExamplePage() {
return <div>Loading this page will throw an error</div>;
}
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.
Then, open the route in your browser. You should start a trace and trigger an error.
error.tsximport * as Sentry from "@sentry/react-router";
import type { Route } from "./+types/example-page";
export async function loader() {
return Sentry.startSpan(
{
op: "test",
name: "My First Test Transaction",
},
() => {
throw new Error("My first Sentry error!");
},
);
}
export default function ExamplePage() {
return <div>Loading this page will throw an error</div>;
}
import * as Sentry from "@sentry/react-router";
import type { Route } from "./+types/example-page";
export async function loader() {
return Sentry.startSpan(
{
op: "test",
name: "My First Test Transaction",
},
() => {
throw new Error("My first Sentry error!");
},
);
}
export default function ExamplePage() {
return <div>Loading this page will throw an error</div>;
}
To verify that Sentry catches your logs, add some log statements to your application:
Sentry.logger.info("User example action completed");
Sentry.logger.warn("Slow operation detected", {
operation: "data_fetch",
duration: 3500,
});
Sentry.logger.error("Validation failed", {
field: "email",
reason: "Invalid email",
});
Sentry.logger.info("User example action completed");
Sentry.logger.warn("Slow operation detected", {
operation: "data_fetch",
duration: 3500,
});
Sentry.logger.error("Validation failed", {
field: "email",
reason: "Invalid email",
});
Now, head over to your project on Sentry.io to view the collected data (it takes a couple of moments for the data to appear).
At this point, you should have integrated Sentry into your React Router Framework application and should already be sending data to your Sentry project.
Now's a good time to customize your setup and look into more advanced topics. Our next recommended steps for you are:
- Explore practical guides on what to monitor, log, track, and investigate after setup
- Learn how to manually capture errors
- Continue to customize your configuration
- Get familiar with Sentry's product features like tracing, insights, and alerts
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").