Skip to content

Commit 3b7288c

Browse files
feat: TanStack Start docs for 1.0 RC (#15337)
## DESCRIBE YOUR PR This PR changes the TanStack Start docs to be compatible with the current 1.0 RC version. ## IS YOUR CHANGE URGENT? Help us prioritize incoming PRs by letting us know when the change needs to go live. - [ ] Urgent deadline (GA date, etc.): <!-- ENTER DATE HERE --> - [ ] Other deadline: <!-- ENTER DATE HERE --> - [x] None: 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 added an urgent due date to it. 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) ## EXTRA RESOURCES - [Sentry Docs contributor guide](https://docs.sentry.io/contributing/) --------- Co-authored-by: Sarah Mischinger <[email protected]>
1 parent 565a70d commit 3b7288c

File tree

1 file changed

+103
-126
lines changed
  • docs/platforms/javascript/guides/tanstackstart-react

1 file changed

+103
-126
lines changed

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

Lines changed: 103 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: TanStack Start React
44

55
<Alert>
66

7-
This SDK is currently in **ALPHA**. Alpha features are still in progress, may have bugs and might include breaking changes.
7+
This SDK is compatible with TanStack Start 1.0 RC and is currently in **ALPHA**. Alpha features are still in progress, may have bugs and might include breaking changes.
88
Please reach out on [GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose) if you have any feedback or concerns.
99

1010
</Alert>
@@ -50,69 +50,69 @@ pnpm add @sentry/tanstackstart-react
5050

5151
### Configure Client-Side Sentry
5252

53-
Create a `src/client.tsx` file (if you don't already have one) and initialize Sentry:
54-
55-
```tsx {filename:src/client.tsx}
56-
import { hydrateRoot } from "react-dom/client";
57-
import { StartClient } from "@tanstack/react-start";
58-
import { createRouter } from "./router";
59-
60-
import * as Sentry from "@sentry/tanstackstart-react";
61-
62-
const router = createRouter();
63-
64-
Sentry.init({
65-
dsn: "___PUBLIC_DSN___",
66-
67-
// Adds request headers and IP for users, for more info visit:
68-
// https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/configuration/options/#sendDefaultPii
69-
sendDefaultPii: true,
70-
71-
integrations: [
72-
// ___PRODUCT_OPTION_START___ performance
73-
Sentry.tanstackRouterBrowserTracingIntegration(router),
74-
// ___PRODUCT_OPTION_END___ performance
75-
// ___PRODUCT_OPTION_START___ session-replay
76-
Sentry.replayIntegration(),
77-
// ___PRODUCT_OPTION_END___ session-replay
78-
// ___PRODUCT_OPTION_START___ user-feedback
79-
Sentry.feedbackIntegration({
80-
// Additional SDK configuration goes in here, for example:
81-
colorScheme: "system",
82-
}),
83-
// ___PRODUCT_OPTION_END___ user-feedback
84-
],
85-
// ___PRODUCT_OPTION_START___ logs
86-
87-
// Enable logs to be sent to Sentry
88-
enableLogs: true,
89-
// ___PRODUCT_OPTION_END___ logs
90-
91-
// ___PRODUCT_OPTION_START___ performance
92-
// Set tracesSampleRate to 1.0 to capture 100%
93-
// of transactions for tracing.
94-
// We recommend adjusting this value in production.
95-
// Learn more at https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
96-
tracesSampleRate: 1.0,
97-
// ___PRODUCT_OPTION_END___ performance
98-
// ___PRODUCT_OPTION_START___ session-replay
99-
100-
// Capture Replay for 10% of all sessions,
101-
// plus for 100% of sessions with an error.
102-
// Learn more at https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
103-
replaysSessionSampleRate: 0.1,
104-
replaysOnErrorSampleRate: 1.0,
105-
// ___PRODUCT_OPTION_END___ session-replay
106-
});
107-
108-
hydrateRoot(document, <StartClient router={router} />);
53+
Initialize Sentry in your `src/router.tsx` file:
54+
55+
```tsx {diff} {filename:src/router.tsx}
56+
+import * as Sentry from "@sentry/tanstackstart-react";
57+
import { createRouter } from '@tanstack/react-router'
58+
59+
// Create a new router instance
60+
export const getRouter = () => {
61+
const router = createRouter();
62+
63+
+ if (!router.isServer) {
64+
+ Sentry.init({
65+
+ dsn: "___PUBLIC_DSN___",
66+
+
67+
+ // Adds request headers and IP for users, for more info visit:
68+
+ // https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/configuration/options/#sendDefaultPii
69+
+ sendDefaultPii: true,
70+
+
71+
+ integrations: [
72+
+ // ___PRODUCT_OPTION_START___ performance
73+
+ Sentry.tanstackRouterBrowserTracingIntegration(router),
74+
+ // ___PRODUCT_OPTION_END___ performance
75+
+ // ___PRODUCT_OPTION_START___ session-replay
76+
+ Sentry.replayIntegration(),
77+
+ // ___PRODUCT_OPTION_END___ session-replay
78+
+ // ___PRODUCT_OPTION_START___ user-feedback
79+
+ Sentry.feedbackIntegration({
80+
+ // Additional SDK configuration goes in here, for example:
81+
+ colorScheme: "system",
82+
+ }),
83+
+ // ___PRODUCT_OPTION_END___ user-feedback
84+
+ ],
85+
+ // ___PRODUCT_OPTION_START___ logs
86+
+
87+
+ // Enable logs to be sent to Sentry
88+
+ enableLogs: true,
89+
+ // ___PRODUCT_OPTION_END___ logs
90+
+
91+
+ // ___PRODUCT_OPTION_START___ performance
92+
+ // Set tracesSampleRate to 1.0 to capture 100%
93+
+ // of transactions for tracing.
94+
+ // We recommend adjusting this value in production.
95+
+ // Learn more at https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
96+
+ tracesSampleRate: 1.0,
97+
+ // ___PRODUCT_OPTION_END___ performance
98+
+ // ___PRODUCT_OPTION_START___ session-replay
99+
+
100+
+ // Capture Replay for 10% of all sessions,
101+
+ // plus for 100% of sessions with an error.
102+
+ // Learn more at https://docs.sentry.io/platforms/javascript/session-replay/configuration/#general-integration-configuration
103+
+ replaysSessionSampleRate: 0.1,
104+
+ replaysOnErrorSampleRate: 1.0,
105+
+ // ___PRODUCT_OPTION_END___ session-replay
106+
+ });
107+
}
108+
}
109109
```
110110

111-
### Configure Server-Side Sentry
111+
### Configure Server-side Sentry
112112

113-
Next, import and initialize Sentry in `src/server.tsx`:
113+
Create an instrument file `instrument.server.mjs` in the root of your project. In this file, initialize the Sentry SDK for your server:
114114

115-
```tsx {filename:src/server.tsx}
115+
```tsx {filename:instrument.server.mjs}
116116
import * as Sentry from "@sentry/tanstackstart-react";
117117

118118
Sentry.init({
@@ -138,80 +138,49 @@ Sentry.init({
138138
});
139139
```
140140

141-
<OnboardingOption optionId="performance">
142-
143-
### Apply Tracing to SSR
141+
#### Moving the Sentry server config file for production usage
144142

145-
Wrap `createRootRoute` with `wrapCreateRootRouteWithSentry` in `src/routes/__root.tsx` to apply tracing to Server-Side Rendering (SSR):
143+
For production monitoring, you need to move the Sentry server config file to your build output. Since [TanStack Start is designed to work with any hosting provider](https://tanstack.com/start/latest/docs/framework/react/guide/hosting), the exact location will depend on where your build artifacts are deployed (for example, `"/dist"`, `".output/server"` or a platform-specific directory).
146144

147-
```tsx {filename:src/routes/__root.tsx} {3,6}
148-
import type { ReactNode } from "react";
149-
import { createRootRoute } from "@tanstack/react-router";
150-
import { wrapCreateRootRouteWithSentry } from "@sentry/tanstackstart-react";
145+
For example, when using [Nitro](https://nitro.build/), copy the instrumentation file to `".output/server"`:
151146

152-
// (Wrap `createRootRoute`, not its return value!)
153-
export const Route = wrapCreateRootRouteWithSentry(createRootRoute)({
154-
// ...
155-
});
147+
```json {diff} {filename:package.json}
148+
{
149+
"scripts": {
150+
- "build": "vite build",
151+
+ "build": "vite build && cp instrument.server.mjs .output/server",
152+
}
153+
}
156154
```
157155

158-
</OnboardingOption>
159-
160-
## Step 3: Capture TanStack Start React Errors
161-
162-
### Instrument Server Requests
156+
#### Load Instrumentation on Startup
163157

164-
Wrap the default stream handler with `wrapStreamHandlerWithSentry` in `src/server.tsx` to instrument requests to your server:
158+
Add a `--import` flag directly or to the `NODE_OPTIONS` environment variable wherever you run your application to import `instrument.server.mjs`.
165159

166-
```tsx {filename:src/server.tsx} {12}
167-
import {
168-
createStartHandler,
169-
defaultStreamHandler,
170-
} from "@tanstack/react-start/server";
171-
import { getRouterManifest } from "@tanstack/react-start/router-manifest";
172-
import { createRouter } from "./router";
173-
import * as Sentry from "@sentry/tanstackstart-react";
174-
175-
export default createStartHandler({
176-
createRouter,
177-
getRouterManifest,
178-
})(Sentry.wrapStreamHandlerWithSentry(defaultStreamHandler));
160+
```json {diff} {filename:package.json}
161+
{
162+
"scripts": {
163+
"build": "vite build && cp instrument.server.mjs .output/server",
164+
- "dev": "vite dev --port 3000",
165+
- "start": "node .output/server/index.mjs",
166+
+ "dev": "NODE_OPTIONS='--import ./instrument.server.mjs' vite dev --port 3000",
167+
+ "start": "node --import ./.output/server/instrument.server.mjs .output/server/index.mjs",
168+
}
169+
}
179170
```
180171

181-
### Instrument Server Functions
182-
183-
Add the Sentry middleware handler to your global middlewares in `src/global-middleware.ts` to instrument your server function invocations:
184-
185-
<Alert level="info">
186-
187-
If you haven't created `src/global-middleware.ts` file, follow the [TanStack Start documentation for Global Middleware](https://tanstack.com/start/latest/docs/framework/react/middleware#global-middleware) to set it up.
188-
189-
</Alert>
190-
191-
```ts {filename:src/global-middleware.ts}
192-
import {
193-
createMiddleware,
194-
registerGlobalMiddleware,
195-
} from "@tanstack/react-start";
196-
import * as Sentry from "@sentry/tanstackstart-react";
197-
198-
registerGlobalMiddleware({
199-
middleware: [
200-
createMiddleware({ type: "function" }).server(
201-
Sentry.sentryGlobalServerMiddlewareHandler()
202-
),
203-
],
204-
});
205-
```
172+
## Step 3: Capture TanStack Start React Errors
206173

207-
### Capturing Errors in Error Boundaries and Components (Optional)
174+
### Instrument Server Requests and Server Functions
208175

209176
<Alert level="info">
210177
Automatic error monitoring is not yet supported on the server side of TanStack
211178
Start. Use `captureException` to manually capture errors in your server-side
212179
code.
213180
</Alert>
214181

182+
### Capturing Errors in Error Boundaries and Components (Optional)
183+
215184
Sentry automatically captures unhandled client-side errors. Errors caught by your own error boundaries aren't captured unless you report them manually:
216185

217186
#### Custom Error Boundary
@@ -287,18 +256,26 @@ To verify that Sentry captures errors and creates issues in your Sentry project,
287256
<OnboardingOption optionId="performance">
288257
### Tracing
289258

290-
To test tracing, create a new file like `src/routes/sentry-example.ts` to create a test route `/sentry-example`:
259+
To test tracing, create a new file like `src/routes/api/sentry-example.ts` to create a test route `/sentry-example`:
291260

292-
```typescript {filename:src/routes/sentry-example.ts}
293-
import { createServerFileRoute } from "@tanstack/react-start/server";
261+
```typescript {filename:src/routes/api/sentry-example.ts}
262+
import { createFileRoute } from "@tanstack/react-router";
294263
import { json } from "@tanstack/react-start";
295264

296-
export const ServerRoute = createServerFileRoute("/sentry-example").methods({
297-
GET: async ({ request }) => {
298-
throw new Error("Sentry Example Route Error");
299-
return json({ message: "Testing Sentry Error..." });
265+
export const Route = createFileRoute('/api/sentry-example')({
266+
server: {
267+
handlers: {
268+
GET: () => {
269+
throw new Error("Sentry Example Route Error");
270+
return new Response(JSON.stringify({ message: "Testing Sentry Error..." }), {
271+
headers: {
272+
'Content-Type': 'application/json',
273+
},
274+
})
275+
},
276+
},
300277
},
301-
});
278+
})
302279
```
303280

304281
Next, update your test button to call this route and throw an error if the response isn't `ok`:
@@ -313,7 +290,7 @@ Next, update your test button to call this route and throw an error if the respo
313290
op: "test",
314291
},
315292
async () => {
316-
const res = await fetch("/sentry-example");
293+
const res = await fetch("/api/sentry-example");
317294
if (!res.ok) {
318295
throw new Error("Sentry Example Frontend Error");
319296
}

0 commit comments

Comments
 (0)