Skip to content

Commit d69d9b5

Browse files
committed
feat: TanStack Start docs for 1.0 RC
1 parent a6d5133 commit d69d9b5

File tree

1 file changed

+104
-128
lines changed
  • docs/platforms/javascript/guides/tanstackstart-react

1 file changed

+104
-128
lines changed

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

Lines changed: 104 additions & 128 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,48 @@ Sentry.init({
138138
});
139139
```
140140

141-
<OnboardingOption optionId="performance">
142-
143-
### Apply Tracing to SSR
144-
145-
Wrap `createRootRoute` with `wrapCreateRootRouteWithSentry` in `src/routes/__root.tsx` to apply tracing to Server-Side Rendering (SSR):
141+
#### Load Instrumentation on Startup
146142

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";
143+
Add a `--import` flag directly or to the `NODE_OPTIONS` environment variable wherever you run your application to import `instrument.server.mjs`.
151144

152-
// (Wrap `createRootRoute`, not its return value!)
153-
export const Route = wrapCreateRootRouteWithSentry(createRootRoute)({
154-
// ...
155-
});
145+
```json {filename:package.json}
146+
{
147+
"scripts": {
148+
"dev": "NODE_OPTIONS='--import ./instrument.server.mjs' vite dev --port 3000",
149+
"start": "node --import ./.output/server/instrument.server.mjs .output/server/index.mjs",
150+
}
151+
}
156152
```
157153

158-
</OnboardingOption>
159-
160-
## Step 3: Capture TanStack Start React Errors
154+
#### Moving the Sentry server config file for production usage
161155

162-
### Instrument Server Requests
156+
For production monitoring, the Sentry server config file needs to be moved 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).
163157

164-
Wrap the default stream handler with `wrapStreamHandlerWithSentry` in `src/server.tsx` to instrument requests to your server:
158+
For example, when using [nitro](https://nitro.build/), copy the instrumentation file to `".output/server"`.
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",
164+
+ "build": "vite build && cp instrument.server.mjs .output/server",
165+
"dev": "NODE_OPTIONS='--import ./instrument.server.mjs' vite dev --port 3000",
166+
"start": "node --import ./.output/server/instrument.server.mjs .output/server/index.mjs",
167+
}
168+
}
179169
```
180170

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-
```
171+
## Step 3: Capture TanStack Start React Errors
206172

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

209175
<Alert level="info">
210176
Automatic error monitoring is not yet supported on the server side of TanStack
211177
Start. Use `captureException` to manually capture errors in your server-side
212178
code.
213179
</Alert>
214180

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

217185
#### Custom Error Boundary
@@ -255,11 +223,11 @@ const route = createRoute({
255223
})
256224
```
257225

258-
## Step 4: Avoid Ad Blockers With Tunneling (Optional)
226+
## Step 3: Avoid Ad Blockers With Tunneling (Optional)
259227

260228
<PlatformContent includePath="getting-started-tunneling" />
261229

262-
## Step 5: Verify
230+
## Step 4: Verify
263231

264232
Let's test your setup and confirm that Sentry is working correctly and sending data to your Sentry project.
265233

@@ -287,18 +255,26 @@ To verify that Sentry captures errors and creates issues in your Sentry project,
287255
<OnboardingOption optionId="performance">
288256
### Tracing
289257

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

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

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..." });
264+
export const Route = createFileRoute('/api/sentry-example')({
265+
server: {
266+
handlers: {
267+
GET: () => {
268+
throw new Error("Sentry Example Route Error");
269+
return new Response(JSON.stringify({ message: "Testing Sentry Error..." }), {
270+
headers: {
271+
'Content-Type': 'application/json',
272+
},
273+
})
274+
},
275+
},
300276
},
301-
});
277+
})
302278
```
303279

304280
Next, update your test button to call this route and throw an error if the response isn't `ok`:
@@ -313,7 +289,7 @@ Next, update your test button to call this route and throw an error if the respo
313289
op: "test",
314290
},
315291
async () => {
316-
const res = await fetch("/sentry-example");
292+
const res = await fetch("/api/sentry-example");
317293
if (!res.ok) {
318294
throw new Error("Sentry Example Frontend Error");
319295
}

0 commit comments

Comments
 (0)