TanStack Start on Cloudflare

Learn how to instrument your TanStack Start app on Cloudflare Workers and capture your first errors with Sentry.

Choose the features you want to configure, and this guide will show you how:

Want to learn more about these features?
  • Issues (always enabled): Sentry's core error monitoring product that automatically reports errors, uncaught exceptions, and unhandled rejections. If you have something that looks like an exception, Sentry can capture it.
  • Tracing: Track software performance while seeing the impact of errors across multiple systems. For example, distributed tracing allows you to follow a request from the frontend to the backend and back.
  • Session Replay: Get to the root cause of an issue faster by viewing a video-like reproduction of what was happening in the user's browser before, during, and after the problem.
  • Logs: Centralize and analyze your application logs to correlate them with errors and performance issues. Search, filter, and visualize log data to understand what's happening in your applications.
  • User Feedback: Collect feedback directly from users when they encounter errors, allowing them to describe what happened and provide context that helps you understand and resolve issues faster.

Run the command for your preferred package manager to add the Sentry SDK to your application:

Copied
npm install @sentry/cloudflare --save

When deploying TanStack Start to Cloudflare Workers, you need to wrap the server entry point with Sentry.withSentry from @sentry/cloudflare. TanStack Start allows you to create a custom server entry file for this purpose.

Create a src/server.ts file that wraps the TanStack Start handler with Sentry.

Use wrapFetchWithSentry from @sentry/tanstackstart-react to instrument TanStack Start server functions with tracing.

src/server.ts
Copied
import * as Sentry from "@sentry/cloudflare";
import { wrapFetchWithSentry } from "@sentry/tanstackstart-react";
import handler from "@tanstack/react-start/server-entry";

export default Sentry.withSentry(
  () => ({
    dsn: "___PUBLIC_DSN___",

    dataCollection: {
      // To disable sending user data and HTTP bodies, uncomment the lines below. For more info visit:
      // https://docs.sentry.io/platforms/javascript/guides/cloudflare/configuration/options/#dataCollection
      // userInfo: false,
      // httpBodies: [],
    },
    // ___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 spans for tracing.
    tracesSampleRate: 1.0,
    // ___PRODUCT_OPTION_END___ performance
  }),
  // @ts-expect-error - handler is not typed as a Cloudflare handler
  wrapFetchWithSentry(handler),
);

Update your wrangler.jsonc (or wrangler.toml) to use your custom server entry:

wrangler.jsonc
Copied
 {
   "name": "my-tanstack-app",
     "main": "src/server.ts",
   // ... rest of config
 }

Initialize Sentry in your src/router.tsx file for client-side error tracking:

src/router.tsx
Copied
+import * as Sentry from "@sentry/tanstackstart-react";
 import { createRouter } from '@tanstack/react-router'

export const getRouter = () => {
  const router = createRouter();

+ if (!router.isServer) {
+   Sentry.init({
+     dsn: "___PUBLIC_DSN___",
+
+     dataCollection: {
+       // To disable sending user data and HTTP bodies, uncomment the lines below. For more info visit:
+       // https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/configuration/options/#dataCollection
+       // userInfo: false,
+       // httpBodies: [],
+     },
+
+     integrations: [
+       // ___PRODUCT_OPTION_START___ performance
+       Sentry.tanstackRouterBrowserTracingIntegration(router),
+       // ___PRODUCT_OPTION_END___ performance
+       // ___PRODUCT_OPTION_START___ session-replay
+       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 spans for tracing.
+     tracesSampleRate: 1.0,
+     // ___PRODUCT_OPTION_END___ performance
+     // ___PRODUCT_OPTION_START___ session-replay
+
+     // Capture Replay for 10% of all sessions,
+     // plus for 100% of sessions with an error.
+     replaysSessionSampleRate: 0.1,
+     replaysOnErrorSampleRate: 1.0,
+     // ___PRODUCT_OPTION_END___ session-replay
+   });
+ }

  return router;
}

To capture server-side errors from HTTP requests and server function calls, add Sentry's global middlewares to createStart() in your src/start.ts file.

The Sentry middleware should be the first middleware in the arrays to ensure all errors are captured.

src/start.ts
Copied
import {
  sentryGlobalFunctionMiddleware,
  sentryGlobalRequestMiddleware,
} from "@sentry/tanstackstart-react";
import { createStart } from "@tanstack/react-start";

export const startInstance = createStart(() => {
  return {
    requestMiddleware: [sentryGlobalRequestMiddleware],
    functionMiddleware: [sentryGlobalFunctionMiddleware],
  };
});

To verify Sentry is capturing errors, add a test button to one of your pages:

Copied
<button
  type="button"
  onClick={() => {
    throw new Error("Sentry Test Error");
  }}
>
  Break the world
</button>;

Run your app with wrangler dev (or through your Vite build with Cloudflare plugin), click the button, and check your Sentry project for the error.

Need help locating the captured errors in your Sentry project?
  • Open the Issues page and select an error from the issues list to view the full details and context of this error. For more details, see this interactive walkthrough.
  • Open the Traces page and select a trace to reveal more information about each span, its duration, and any errors. For an interactive UI walkthrough, click here.
  • Open the Logs page and filter by service, environment, or search keywords to view log entries from your application. For an interactive UI walkthrough, click here.

At this point, you should have integrated Sentry 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:

Are you having problems setting up the SDK?
Was this helpful?
Help improve this content
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").