TanStack Start React
Learn how to set up and configure Sentry in your TanStack Start React application, capturing your first errors, and viewing them in Sentry.
This SDK is currently in ALPHA. Alpha features are still in progress, may have bugs and might include breaking changes. Please reach out on GitHub if you have any feedback or concerns.
This guide walks you through setting up Sentry in a TanStack Start (React) app. For TanStack Router (React), see our React TanStack Router guide.
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/tanstackstart-react --save
Create a src/client.tsx
file (if you don't already have one) and initialize Sentry:
src/client.tsx
import { hydrateRoot } from "react-dom/client";
import { StartClient } from "@tanstack/react-start";
import { createRouter } from "./router";
import * as Sentry from "@sentry/tanstackstart-react";
const router = createRouter();
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0example-org / example-project",
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/configuration/options/#sendDefaultPii
sendDefaultPii: true,
integrations: [
// performance
Sentry.tanstackRouterBrowserTracingIntegration(router),
// performance
// session-replay
Sentry.replayIntegration(),
// session-replay
// user-feedback
Sentry.feedbackIntegration({
// Additional SDK configuration goes in here, for example:
colorScheme: "system",
}),
// user-feedback
],
// logs
// Enable logs to be sent to Sentry
enableLogs: true,
// logs
// 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/configuration/options/#traces-sample-rate
tracesSampleRate: 1.0,
// performance
// session-replay
// Capture Replay for 10% of all sessions,
// plus for 100% of sessions with an error.
// Learn more at https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
replaysSessionSampleRate: 0.1,
replaysOnErrorSampleRate: 1.0,
// session-replay
});
hydrateRoot(document, <StartClient router={router} />);
Next, import and initialize Sentry in src/server.tsx
:
src/server.tsx
import * as Sentry from "@sentry/tanstackstart-react";
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0example-org / example-project",
// Adds request headers and IP for users, for more info visit:
// https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/configuration/options/#sendDefaultPii
sendDefaultPii: true,
// logs
// Enable logs to be sent to Sentry
enableLogs: true,
// logs
// 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/configuration/options/#traces-sample-rate
tracesSampleRate: 1.0,
// performance
});
Wrap createRootRoute
with wrapCreateRootRouteWithSentry
in src/routes/__root.tsx
to apply tracing to Server-Side Rendering (SSR):
src/routes/__root.tsx
import type { ReactNode } from "react";
import { createRootRoute } from "@tanstack/react-router";
import { wrapCreateRootRouteWithSentry } from "@sentry/tanstackstart-react";
// (Wrap `createRootRoute`, not its return value!)
export const Route = wrapCreateRootRouteWithSentry(createRootRoute)({
// ...
});
Wrap the default stream handler with wrapStreamHandlerWithSentry
in src/server.tsx
to instrument requests to your server:
src/server.tsx
import {
createStartHandler,
defaultStreamHandler,
} from "@tanstack/react-start/server";
import { getRouterManifest } from "@tanstack/react-start/router-manifest";
import { createRouter } from "./router";
import * as Sentry from "@sentry/tanstackstart-react";
export default createStartHandler({
createRouter,
getRouterManifest,
})(Sentry.wrapStreamHandlerWithSentry(defaultStreamHandler));
Add the Sentry middleware handler to your global middlewares in src/global-middleware.ts
to instrument your server function invocations:
If you haven't created src/global-middleware.ts
file, follow the TanStack Start documentation for Global Middleware to set it up.
src/global-middleware.ts
import {
createMiddleware,
registerGlobalMiddleware,
} from "@tanstack/react-start";
import * as Sentry from "@sentry/tanstackstart-react";
registerGlobalMiddleware({
middleware: [
createMiddleware({ type: "function" }).server(
Sentry.sentryGlobalServerMiddlewareHandler()
),
],
});
Automatic error monitoring is not yet supported on the server side of TanStack Start. Use captureException
to manually capture errors in your server-side code.
Sentry automatically captures unhandled client-side errors. Errors caught by your own error boundaries aren't captured unless you report them manually:
Wrap your custom ErrorBoundary
component with withErrorBoundary
:
import React from "react";
import * as Sentry from "@sentry/tanstackstart-react";
class MyErrorBoundary extends React.Component {
// ...
}
export const MySentryWrappedErrorBoundary = Sentry.withErrorBoundary(
MyErrorBoundary,
{
// ... sentry error wrapper options
},
);
Use Sentry's captureException
function inside a useEffect
hook within your errorComponent
:
import { createRoute } from "@tanstack/react-router";
import * as Sentry from "@sentry/tanstackstart-react";
const route = createRoute({
errorComponent: ({ error }) => {
useEffect(() => {
Sentry.captureException(error)
}, [error])
return (
// ...
)
}
})
You can prevent ad blockers from blocking Sentry events using tunneling. Use the tunnel
option to add an API endpoint in your application that forwards Sentry events to Sentry servers.
To enable tunneling, update Sentry.init
with the following option:
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0example-org / example-project",,
tunnel: "/tunnel",
});
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.
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, add a test button to one of your pages, which will trigger an error that Sentry will capture when you click it:
<button
type="button"
onClick={() => {
throw new Error("Sentry Test Error");
}}
>
Break the world
</button>;
Open the page in a browser and click the button to trigger a frontend 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.
To test tracing, create a new file like src/routes/sentry-example.ts
to create a test route /sentry-example
:
src/routes/sentry-example.ts
import { createServerFileRoute } from "@tanstack/react-start/server";
import { json } from "@tanstack/react-start";
export const ServerRoute = createServerFileRoute(
"/sentry-example",
).methods({
GET: async ({ request }) => {
throw new Error("Sentry Example Route Error");
return json({ message: "Testing Sentry Error..." });
},
});
Next, update your test button to call this route and throw an error if the response isn't ok
:
<button
type="button"
onClick={async () => {
await Sentry.startSpan(
{
name: "Example Frontend Span",
op: "test",
},
async () => {
const res = await fetch("/sentry-example");
if (!res.ok) {
throw new Error("Sentry Example Frontend Error");
}
},
);
}}
>
Break the world
</button>;
Open the page in a browser and click the button to trigger two errors:
- a frontend error
- an error within the API route
Additionally, this starts a performance trace to measure the time it takes for the API request to complete.
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 TanStack Start React 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:
- 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").