Skip to content

Commit 738dfdd

Browse files
chargomeclaude
andauthored
docs(tanstackstart-react): Update setup for TanStack Start stable (#20910)
## DESCRIBE YOUR PR Updates the TanStack Start React docs for TanStack Start stable and the current `@sentry/tanstackstart-react` SDK. - Remove the beta alerts and require `@tanstack/react-start` 1.169.0 or newer - Make the automatic `instrument.server` loading in Nitro builds the main path and drop the `cp`/`--import` production steps - Forward request options in `src/server.ts` and add the CSRF middleware back in `start.ts` - Document server function spans (available since 11.15.0) and route `onError` for loader errors - Fix the error boundary and `errorComponent` examples ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. Select exactly one option. For deadlines, replace `YYYY-MM-DD` with the due date. You can update this information later by editing the PR description. - [ ] Urgent deadline (GA date, etc.): YYYY-MM-DD - [ ] Other deadline: YYYY-MM-DD - [x] No deadline: Not urgent, can wait up to 1 week+ ## SLA - Teamwork makes the dream work, so please add a reviewer to your PRs. - Please give the docs team up to 1 week to review your PR unless you've supplied a deadline. Thanks in advance for your help! ## PRE-MERGE CHECKLIST _Make sure you've checked the following before merging your changes:_ - [ ] Checked Vercel preview for correctness, including links - [ ] PR was reviewed and approved by any necessary SMEs (subject matter experts) - [ ] PR was reviewed and approved by a member of the [Sentry docs team](https://github.com/orgs/getsentry/teams/docs) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0eacaa3 commit 738dfdd

5 files changed

Lines changed: 99 additions & 106 deletions

File tree

‎docs/platforms/javascript/guides/tanstackstart-react/features/sentryTanstackStart.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ description: "Learn about the sentryTanstackStart Vite plugin."
77

88
The `sentryTanstackStart` Vite plugin simplifies source map configuration for TanStack Start applications. It automatically handles source map generation and upload to Sentry during production builds.
99

10+
The plugin also loads your `instrument.server` file in production builds that use Nitro for a Node.js runtime. Learn more in [Load the Server Config When the Server Starts](/platforms/javascript/guides/tanstackstart-react/manual-setup/#load-the-server-config-when-the-server-starts).
11+
1012
## Usage
1113

1214
To automatically upload source maps, you need to provide your Sentry auth token, organization, and project slugs in your Vite configuration. The plugin then, by default, automatically enables hidden source maps and deletes `.map` files after upload.

‎docs/platforms/javascript/guides/tanstackstart-react/index.mdx‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ categories:
77
- browser
88
---
99

10-
<Alert level="warning" title="Beta">
11-
12-
This SDK is compatible with TanStack Start 1.0 RC and is currently in **beta**. Beta features are still in progress and may have bugs.
13-
Please reach out on [GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose) if you have any feedback or concerns.
14-
15-
</Alert>
16-
1710
<PlatformContent includePath="llm-rules-platform" />
1811

1912
<PlatformContent includePath="getting-started-prerequisites" />

‎docs/platforms/javascript/guides/tanstackstart-react/manual-setup/cloudflare.mdx‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This guide is for TanStack Start deployed to **Cloudflare Workers** using the `@
1414

1515
</Alert>
1616

17+
<PlatformContent includePath="getting-started-prerequisites" />
18+
1719
<StepConnector selector="h2" showNumbers={true}>
1820

1921
## Install
@@ -145,30 +147,51 @@ export const getRouter = () => {
145147

146148
### Capture Server-Side Errors
147149

148-
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.
150+
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. Since version 11.15.0, `sentryGlobalFunctionMiddleware` also creates a span for every server function call, including calls from other server functions or from loaders on the server. Without this middleware, Sentry doesn't create server function spans.
149151

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

152-
<Alert level="info">
153-
SSR rendering exceptions are not captured by the middleware. Use
154-
`captureException` to manually capture those errors.
155-
</Alert>
154+
A custom `src/start.ts` turns off the CSRF protection that TanStack Start adds to server functions by default, so the snippet adds it back with `createCsrfMiddleware`.
156155

157156
```tsx {filename:src/start.ts}
158157
import {
159158
sentryGlobalFunctionMiddleware,
160159
sentryGlobalRequestMiddleware,
161160
} from "@sentry/tanstackstart-react";
162-
import { createStart } from "@tanstack/react-start";
161+
import { createCsrfMiddleware, createStart } from "@tanstack/react-start";
162+
163+
// Assign the middleware to a variable. Sentry's Vite plugin
164+
// doesn't instrument a middleware array that contains a function call.
165+
const csrfMiddleware = createCsrfMiddleware({
166+
filter: (ctx) => ctx.handlerType === "serverFn",
167+
});
163168

164169
export const startInstance = createStart(() => {
165170
return {
166-
requestMiddleware: [sentryGlobalRequestMiddleware],
171+
requestMiddleware: [sentryGlobalRequestMiddleware, csrfMiddleware],
167172
functionMiddleware: [sentryGlobalFunctionMiddleware],
168173
};
169174
});
170175
```
171176

177+
### Capture Errors in Route Loaders
178+
179+
If a `loader` or `beforeLoad` throws while a page renders on the server, the router catches the error and shows the route's error component. Sentry doesn't report these errors automatically. To report them, set `onError` on the route:
180+
181+
```tsx {filename:src/routes/posts.tsx}
182+
import * as Sentry from "@sentry/tanstackstart-react";
183+
import { createFileRoute } from "@tanstack/react-router";
184+
185+
export const Route = createFileRoute("/posts")({
186+
loader: () => fetchPosts(),
187+
onError: (error) => {
188+
Sentry.captureException(error);
189+
},
190+
});
191+
```
192+
193+
TanStack Router has no router-wide `onError`, so set it on each route.
194+
172195
### Add Readable Stack Traces With Source Maps
173196

174197
<PlatformContent

‎docs/platforms/javascript/guides/tanstackstart-react/manual-setup/index.mdx‎

Lines changed: 60 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ runtime: node
1111

1212
<PlatformContent includePath="sentry-init/manual-setup-callout" />
1313

14-
<Alert level="warning" title="Beta">
15-
16-
This SDK is compatible with TanStack Start 1.0 RC and is currently in **beta**. Beta features are still in progress and may have bugs.
17-
Please reach out on [GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose) if you have any feedback or concerns.
18-
19-
</Alert>
20-
2114
This guide walks you through setting up Sentry in a [TanStack Start (React)](https://tanstack.com/start/latest/docs/framework/react/overview) app on Node.js.
2215
For [TanStack Router (React)](https://tanstack.com/router/latest/docs/framework/react/overview), see our [React TanStack Router guide](/platforms/javascript/guides/react/features/tanstack-router/).
2316

@@ -190,145 +183,120 @@ SENTRY_AUTH_TOKEN=___ORG_AUTH_TOKEN___
190183

191184
To capture server-side errors and tracing data, you need to explicitly define a [server entry point](https://tanstack.com/start/latest/docs/framework/react/guide/server-entry-point) in your application and wrap your request handler with `wrapFetchWithSentry`.
192185

193-
Follow the section below that matches your deployment setup:
194-
195-
#### With `--import` Flag (e.g. Node.js Server)
196-
197-
Use this setup when you can configure Node.js CLI flags.
198-
199-
Create a `src/server.ts` file in your project:
186+
Create a `src/server.ts` file in your project. Pass `opts` on to the handler, so the request context that TanStack Start passes to it isn't lost:
200187

201188
```typescript {filename:src/server.ts}
202189
import { wrapFetchWithSentry } from "@sentry/tanstackstart-react";
203-
import handler, { createServerEntry } from "@tanstack/react-start/server-entry";
204-
205-
export default createServerEntry(
206-
wrapFetchWithSentry({
207-
fetch(request: Request) {
208-
return handler.fetch(request);
209-
},
210-
})
211-
);
190+
import handler, {
191+
createServerEntry,
192+
} from "@tanstack/react-start/server-entry";
193+
import type { ServerEntry } from "@tanstack/react-start/server-entry";
194+
195+
const requestHandler: ServerEntry = wrapFetchWithSentry({
196+
fetch(request, opts) {
197+
return handler.fetch(request, opts);
198+
},
199+
});
200+
201+
export default createServerEntry(requestHandler);
212202
```
213203

214-
##### Move the Sentry Server Config File for Production Usage
204+
#### Load the Server Config When the Server Starts
215205

216-
For production monitoring, you need to move the Sentry server config file to your build output. The exact location depends on where your build artifacts are deployed (for example, `"/dist"`, `".output/server"` or a platform-specific directory). For example, when using Nitro, copy the instrumentation file to `".output/server"` as shown in the example on the right.
206+
<AvailableSince version="11.13.0" />
217207

218-
```json {diff} {filename:package.json}
219-
{
220-
"scripts": {
221-
- "build": "vite build",
222-
+ "build": "vite build && cp instrument.server.mjs .output/server",
223-
}
224-
}
225-
```
208+
If your app builds with [Nitro](https://nitro.build/) for a Node.js runtime, the `sentryTanstackStart` Vite plugin loads your server config for you in production builds:
226209

227-
##### Load Instrumentation on Startup
210+
- It looks for `instrument.server.{ts,mts,js,mjs}` in the project root, then in `src/`.
211+
- It bundles the file into the Nitro server build and loads it when the server starts.
212+
- It instruments database drivers and other libraries at build time.
228213

229-
To load `instrument.server.mjs`, add the `--import` flag either directly to your run command or to the `NODE_OPTIONS` environment variable.
214+
You start your production build as usual, without the `--import` flag.
215+
216+
`vite dev` doesn't create a server build, so in development, load the file with the `--import` flag in the `NODE_OPTIONS` environment variable:
230217

231218
```json {diff} {filename:package.json}
232219
{
233220
"scripts": {
234-
"build": "vite build && cp instrument.server.mjs .output/server",
235-
- "dev": "vite dev --port 3000",
236-
- "start": "node .output/server/index.mjs",
237-
+ "dev": "NODE_OPTIONS='--import ./instrument.server.mjs' vite dev --port 3000",
238-
+ "start": "node --import ./.output/server/instrument.server.mjs .output/server/index.mjs",
221+
"build": "vite build",
222+
- "dev": "vite dev --port 3000",
223+
+ "dev": "NODE_OPTIONS='--import ./instrument.server.mjs' vite dev --port 3000",
224+
"start": "node .output/server/index.mjs",
239225
}
240226
}
241227
```
242228

243-
#### Without `--import` Flag
244-
245-
Use this setup when you can't configure Node.js CLI flags or environment variables at runtime (for example, when deploying to serverless platforms like Vercel or Netlify).
246-
247-
<Alert level='warning' title='Restrictions'>
229+
This doesn't apply to Cloudflare. For Cloudflare Workers, follow the [Cloudflare guide](/platforms/javascript/guides/tanstackstart-react/manual-setup/cloudflare/).
248230

249-
You can only instrument native Node.js APIs (such as `fetch` and the `http` module) with this installation method.
250-
This means that the Sentry SDK will not capture data from database calls, queues, ORMs, third-party libraries, or other framework-specific data.
251-
Thus, we recommend you follow the [`--import` flag setup instructions](#with---import-flag-eg-nodejs-server) if possible.
231+
##### Without Nitro
252232

253-
This setup doesn't work for Cloudflare deployments.
254-
255-
</Alert>
256-
257-
Create a `src/server.ts` file in your project and import the server instrumentation file directly at the top of your server entry point.
258-
259-
```typescript {filename:src/server.ts}
260-
import "../instrument.server.mjs";
261-
import { wrapFetchWithSentry } from "@sentry/tanstackstart-react";
262-
import handler, { createServerEntry } from "@tanstack/react-start/server-entry";
263-
264-
export default createServerEntry(
265-
wrapFetchWithSentry({
266-
fetch(request: Request) {
267-
return handler.fetch(request);
268-
},
269-
})
270-
);
271-
```
233+
The plugin loads the server config through a Nitro hook. If you deploy to Node.js without Nitro, load `instrument.server.mjs` yourself: add the `--import` flag to your start command, or import the file at the top of `src/server.ts`.
272234

273235
### Capture TanStack Start React Errors
274236

275237
#### Capture Server-Side Errors
276238

277-
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.
239+
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. Since version 11.15.0, `sentryGlobalFunctionMiddleware` also creates a span for every server function call, including calls from other server functions or from loaders on the server. Without this middleware, Sentry doesn't create server function spans.
278240

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

281-
<Alert level="info">
282-
SSR rendering exceptions are not captured by the middleware. Use
283-
`captureException` to manually capture those errors.
284-
</Alert>
243+
A custom `src/start.ts` turns off the CSRF protection that TanStack Start adds to server functions by default, so the snippet adds it back with `createCsrfMiddleware`.
285244

286245
```tsx {filename:src/start.ts}
287246
import {
288247
sentryGlobalFunctionMiddleware,
289248
sentryGlobalRequestMiddleware,
290249
} from "@sentry/tanstackstart-react";
291-
import { createStart } from "@tanstack/react-start";
250+
import { createCsrfMiddleware, createStart } from "@tanstack/react-start";
251+
252+
// Assign the middleware to a variable. Sentry's Vite plugin
253+
// doesn't instrument a middleware array that contains a function call.
254+
const csrfMiddleware = createCsrfMiddleware({
255+
filter: (ctx) => ctx.handlerType === "serverFn",
256+
});
292257

293258
export const startInstance = createStart(() => {
294259
return {
295-
requestMiddleware: [sentryGlobalRequestMiddleware],
260+
requestMiddleware: [sentryGlobalRequestMiddleware, csrfMiddleware],
296261
functionMiddleware: [sentryGlobalFunctionMiddleware],
297262
};
298263
});
299264
```
300265

301-
#### Capture Errors in Error Boundaries and Components (Optional)
266+
#### Capture Errors in Route Loaders
302267

303-
Sentry automatically captures unhandled client-side errors. Errors caught by your own error boundaries aren't captured unless you report them manually:
268+
If a `loader` or `beforeLoad` throws while a page renders on the server, the router catches the error and shows the route's error component. Sentry doesn't report these errors automatically. To report them, set `onError` on the route:
304269

305-
##### Custom Error Boundary
270+
```tsx {filename:src/routes/posts.tsx}
271+
import * as Sentry from "@sentry/tanstackstart-react";
272+
import { createFileRoute } from "@tanstack/react-router";
306273

307-
Wrap your custom `ErrorBoundary` component with [`withErrorBoundary`](/platforms/javascript/guides/react/features/error-boundary/):
274+
export const Route = createFileRoute("/posts")({
275+
loader: () => fetchPosts(),
276+
onError: (error) => {
277+
Sentry.captureException(error);
278+
},
279+
});
280+
```
308281

309-
```tsx
310-
import React from "react";
311-
import * as Sentry from "@sentry/tanstackstart-react";
282+
TanStack Router has no router-wide `onError`, so set it on each route.
312283

313-
class MyErrorBoundary extends React.Component {
314-
// ...
315-
}
284+
#### Capture Errors in Error Boundaries and Components (Optional)
316285

317-
export const MySentryWrappedErrorBoundary = Sentry.withErrorBoundary(
318-
MyErrorBoundary,
319-
{
320-
// ... sentry error wrapper options
321-
}
322-
);
323-
```
286+
Sentry automatically captures unhandled client-side errors. Errors caught by your own error boundaries aren't captured unless you report them manually:
287+
288+
##### Custom Error Boundary
289+
290+
To report the errors that an error boundary catches, use Sentry's `ErrorBoundary` component instead of your own. Learn more in [React Error Boundary](/platforms/javascript/guides/react/features/error-boundary/).
324291

325292
##### TanStack Router `errorComponent`
326293

327294
Use Sentry's `captureException` function inside a `useEffect` hook within your `errorComponent`:
328295

329-
```tsx {2,6-8}
296+
```tsx {2-3,7-9}
330297
import { createRoute } from "@tanstack/react-router";
331298
import * as Sentry from "@sentry/tanstackstart-react";
299+
import { useEffect } from "react";
332300

333301
const route = createRoute({
334302
errorComponent: ({ error }) => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Prerequisites
2+
3+
You need:
4+
5+
- A Sentry [account](https://sentry.io/signup/) and [project](/product/projects/)
6+
- Your application up and running
7+
- `@tanstack/react-start` 1.169.0 or newer

0 commit comments

Comments
 (0)