Skip to content

Commit b9eafdf

Browse files
chargomeclaude
andauthored
feat(onboarding): Update TanStack Start setup to match the docs (#132137)
Update the TanStack Start React onboarding to match the new setup in the docs (getsentry/sentry-docs#20910). - With Nitro, the Vite plugin loads `instrument.server` at startup, so the `--import` flag is only needed for `vite dev` - Fix the dead docs link for setups without `--import` - Forward `opts` in `src/server.ts` - Add `createCsrfMiddleware` to `src/start.ts` - Replace the outdated error boundary, `errorComponent` and tunneling snippets with a link to the docs, like Astro and SolidStart do Refs JSSDK-59 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5d8224e commit b9eafdf

2 files changed

Lines changed: 89 additions & 146 deletions

File tree

‎static/app/gettingStartedDocs/javascript-tanstackstart-react/onboarding.spec.tsx‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,46 @@ describe('javascript-tanstackstart-react onboarding docs', () => {
2929
).toBeInTheDocument();
3030
});
3131

32+
it('only uses the --import flag for vite dev', () => {
33+
renderWithOnboardingLayout(docs);
34+
35+
expect(
36+
screen.getByText(
37+
textWithMarkupMatcher(
38+
/NODE_OPTIONS='--import \.\/instrument\.server\.mjs' vite dev/
39+
)
40+
)
41+
).toBeInTheDocument();
42+
expect(
43+
screen.getByText(
44+
textWithMarkupMatcher(/"start": "node \.output\/server\/index\.mjs"/)
45+
)
46+
).toBeInTheDocument();
47+
expect(
48+
screen.queryByText(textWithMarkupMatcher(/cp instrument\.server\.mjs/))
49+
).not.toBeInTheDocument();
50+
});
51+
52+
it('forwards opts in the server entry', () => {
53+
renderWithOnboardingLayout(docs);
54+
55+
expect(
56+
screen.getByText(textWithMarkupMatcher(/return handler\.fetch\(request, opts\)/))
57+
).toBeInTheDocument();
58+
});
59+
60+
it('adds the CSRF middleware in src/start.ts', () => {
61+
renderWithOnboardingLayout(docs);
62+
63+
expect(
64+
screen.getByText(
65+
textWithMarkupMatcher(
66+
/requestMiddleware: \[sentryGlobalRequestMiddleware, csrfMiddleware\]/
67+
)
68+
)
69+
).toBeInTheDocument();
70+
});
71+
3272
it('displays sample rates when performance and replay are selected', () => {
3373
renderWithOnboardingLayout(docs, {
3474
selectedProducts: [

‎static/app/gettingStartedDocs/javascript-tanstackstart-react/onboarding.tsx‎

Lines changed: 49 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,6 @@ ${
210210
},
211211
],
212212
},
213-
{
214-
type: 'alert',
215-
alertType: 'warning',
216-
showIcon: true,
217-
text: tct(
218-
"If you can't use the [code:--import] flag (e.g. in serverless environments like Vercel or Netlify), follow the [link:TanStack Start React guide] for alternative setup instructions.",
219-
{
220-
code: <code />,
221-
link: (
222-
<ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/#without---import-flag-eg-vercel-netlify" />
223-
),
224-
}
225-
),
226-
},
227213
{
228214
type: 'text',
229215
text: tct(
@@ -238,9 +224,10 @@ ${
238224
},
239225
{
240226
type: 'text',
241-
text: tct('Create a [code:src/server.ts] file in your project:', {
242-
code: <code />,
243-
}),
227+
text: tct(
228+
"Create a [code:src/server.ts] file in your project. Pass [code:opts] on to the handler, so the request context that TanStack Start passes to it isn't lost:",
229+
{code: <code />}
230+
),
244231
},
245232
{
246233
type: 'code',
@@ -250,15 +237,18 @@ ${
250237
language: 'typescript',
251238
filename: 'src/server.ts',
252239
code: `import { wrapFetchWithSentry } from "@sentry/tanstackstart-react";
253-
import handler, { createServerEntry } from "@tanstack/react-start/server-entry";
240+
import handler, {
241+
createServerEntry,
242+
} from "@tanstack/react-start/server-entry";
243+
import type { ServerEntry } from "@tanstack/react-start/server-entry";
254244
255-
export default createServerEntry(
256-
wrapFetchWithSentry({
257-
fetch(request: Request) {
258-
return handler.fetch(request);
259-
},
260-
})
261-
);`,
245+
const requestHandler: ServerEntry = wrapFetchWithSentry({
246+
fetch(request, opts) {
247+
return handler.fetch(request, opts);
248+
},
249+
});
250+
251+
export default createServerEntry(requestHandler);`,
262252
},
263253
],
264254
},
@@ -283,6 +273,7 @@ import { tanstackStart } from "@tanstack/react-start/plugin/vite";
283273
export default defineConfig({
284274
plugins: [
285275
tanstackStart(),
276+
// other plugins - sentryTanstackStart should be last
286277
sentryTanstackStart({
287278
org: "${params.organization.slug}",
288279
project: "${params.project.slug}",
@@ -296,20 +287,15 @@ export default defineConfig({
296287
{
297288
type: 'text',
298289
text: tct(
299-
'For production monitoring, you need to move the Sentry server config file to your build output. Since [hostingLink:TanStack Start is designed to work with any hosting provider], the exact location will depend on where your build artifacts are deployed (for example, [code:/dist], [code:.output/server] or a platform-specific directory).',
300-
{
301-
code: <code />,
302-
hostingLink: (
303-
<ExternalLink href="https://tanstack.com/start/latest/docs/framework/react/guide/hosting" />
304-
),
305-
}
290+
'If your app builds with [nitroLink:Nitro] for a Node.js runtime, the [code:sentryTanstackStart] Vite plugin bundles [code:instrument.server.mjs] into the server build and loads it when the server starts. You start your production build as usual, without the [code:--import] flag.',
291+
{code: <code />, nitroLink: <ExternalLink href="https://nitro.build/" />}
306292
),
307293
},
308294
{
309295
type: 'text',
310296
text: tct(
311-
'For example, when using [nitroLink:Nitro], copy the instrumentation file to [code:.output/server]:',
312-
{code: <code />, nitroLink: <ExternalLink href="https://nitro.build/" />}
297+
"[code:vite dev] doesn't create a server build, so in development, load the file with the [code:--import] flag in the [code:NODE_OPTIONS] environment variable:",
298+
{code: <code />}
313299
),
314300
},
315301
{
@@ -321,40 +307,31 @@ export default defineConfig({
321307
filename: 'package.json',
322308
code: `{
323309
"scripts": {
324-
"build": "vite build && cp instrument.server.mjs .output/server",
310+
"build": "vite build",
311+
"dev": "NODE_OPTIONS='--import ./instrument.server.mjs' vite dev --port 3000",
312+
"start": "node .output/server/index.mjs",
325313
}
326314
}`,
327315
},
328316
],
329317
},
330318
{
331-
type: 'text',
319+
type: 'alert',
320+
alertType: 'info',
321+
showIcon: true,
332322
text: tct(
333-
'Add a [code:--import] flag directly or to the [code:NODE_OPTIONS] environment variable wherever you run your application to import [code:instrument.server.mjs]:',
334-
{code: <code />}
335-
),
336-
},
337-
{
338-
type: 'code',
339-
tabs: [
323+
"If you deploy to Node.js without Nitro, or to Cloudflare, the plugin doesn't load the server config for you. Follow the [link:TanStack Start React guide] for these setups.",
340324
{
341-
label: 'JSON',
342-
language: 'json',
343-
filename: 'package.json',
344-
code: `{
345-
"scripts": {
346-
"build": "vite build && cp instrument.server.mjs .output/server",
347-
"dev": "NODE_OPTIONS='--import ./instrument.server.mjs' vite dev --port 3000",
348-
"start": "node --import ./.output/server/instrument.server.mjs .output/server/index.mjs",
349-
}
350-
}`,
351-
},
352-
],
325+
link: (
326+
<ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/manual-setup/#load-the-server-config-when-the-server-starts" />
327+
),
328+
}
329+
),
353330
},
354331
{
355332
type: 'text',
356333
text: tct(
357-
"To capture server-side errors from HTTP requests and server function invocations, add Sentry's global middlewares to [code:createStart()] in your [code:src/start.ts] file:",
334+
"To capture server-side errors from HTTP requests and server function invocations, add Sentry's global middlewares to [code:createStart()] in your [code:src/start.ts] file. A custom [code:src/start.ts] turns off the CSRF protection that TanStack Start adds to server functions by default, so the snippet adds it back with [code:createCsrfMiddleware]:",
358335
{code: <code />}
359336
),
360337
},
@@ -369,11 +346,17 @@ export default defineConfig({
369346
sentryGlobalFunctionMiddleware,
370347
sentryGlobalRequestMiddleware,
371348
} from "@sentry/tanstackstart-react";
372-
import { createStart } from "@tanstack/react-start";
349+
import { createCsrfMiddleware, createStart } from "@tanstack/react-start";
350+
351+
// Assign the middleware to a variable. Sentry's Vite plugin
352+
// doesn't instrument a middleware array that contains a function call.
353+
const csrfMiddleware = createCsrfMiddleware({
354+
filter: (ctx) => ctx.handlerType === "serverFn",
355+
});
373356
374357
export const startInstance = createStart(() => {
375358
return {
376-
requestMiddleware: [sentryGlobalRequestMiddleware],
359+
requestMiddleware: [sentryGlobalRequestMiddleware, csrfMiddleware],
377360
functionMiddleware: [sentryGlobalFunctionMiddleware],
378361
};
379362
});`,
@@ -385,68 +368,21 @@ export const startInstance = createStart(() => {
385368
alertType: 'info',
386369
showIcon: false,
387370
text: t(
388-
'The Sentry middleware should be the first middleware in the arrays to ensure all errors are captured. SSR rendering exceptions are not captured by the middleware. Use captureException to manually capture those errors.'
371+
'The Sentry middleware should be the first middleware in the arrays to ensure all errors are captured.'
389372
),
390373
},
391374
{
392375
type: 'text',
393376
text: tct(
394-
"Errors caught by your own error boundaries aren't captured unless you report them manually. Wrap your custom [code:ErrorBoundary] component with [code:withErrorBoundary]:",
395-
{code: <code />}
396-
),
397-
},
398-
{
399-
type: 'code',
400-
tabs: [
377+
'To capture errors from route loaders, error boundaries and [code:errorComponent]s, and to send events through a tunnel to avoid ad blockers, follow the [link:TanStack Start React guide].',
401378
{
402-
label: 'TypeScript',
403-
language: 'tsx',
404-
code: `import React from "react";
405-
import * as Sentry from "@sentry/tanstackstart-react";
406-
407-
class MyErrorBoundary extends React.Component {
408-
// ...
409-
}
410-
411-
export const MySentryWrappedErrorBoundary = withErrorBoundary(
412-
MyErrorBoundary,
413-
{
414-
// ... sentry error wrapper options
415-
},
416-
);`,
417-
},
418-
],
419-
},
420-
{
421-
type: 'text',
422-
text: tct(
423-
'If you defined [code:errorComponents] in your Code-Based TanStack Router routes, capture the [code:error] argument with [code:captureException] inside a [code:useEffect] hook:',
424-
{code: <code />}
379+
code: <code />,
380+
link: (
381+
<ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/manual-setup/#capture-tanstack-start-react-errors" />
382+
),
383+
}
425384
),
426385
},
427-
{
428-
type: 'code',
429-
tabs: [
430-
{
431-
label: 'TypeScript',
432-
language: 'tsx',
433-
code: `import { createRoute } from "@tanstack/react-router";
434-
import * as Sentry from "@sentry/tanstackstart-react";
435-
436-
const route = createRoute({
437-
errorComponent: ({ error }) => {
438-
useEffect(() => {
439-
Sentry.captureException(error)
440-
}, [error])
441-
442-
return (
443-
// ...
444-
)
445-
}
446-
})`,
447-
},
448-
],
449-
},
450386
],
451387
},
452388
{
@@ -473,39 +409,6 @@ const route = createRoute({
473409
],
474410
collapsible: true,
475411
},
476-
{
477-
title: t('Avoid Ad Blockers With Tunneling (Optional)'),
478-
content: [
479-
{
480-
type: 'text',
481-
text: tct(
482-
'You can prevent ad blockers from blocking Sentry events using tunneling. Use the [code:tunnel] option to add an API endpoint in your application that forwards Sentry events to Sentry servers.',
483-
{code: <code />}
484-
),
485-
},
486-
{
487-
type: 'text',
488-
text: tct(
489-
'To enable tunneling, update [code:Sentry.init] with the following option:',
490-
{code: <code />}
491-
),
492-
},
493-
{
494-
type: 'code',
495-
tabs: [
496-
{
497-
label: 'TypeScript',
498-
language: 'tsx',
499-
code: `Sentry.init({
500-
dsn: "${params.dsn.public}",
501-
tunnel: '/tunnel',
502-
});`,
503-
},
504-
],
505-
},
506-
],
507-
collapsible: true,
508-
},
509412
getDataCollectionStep({
510413
docsLink:
511414
'https://docs.sentry.io/platforms/javascript/guides/tanstackstart-react/configuration/options/#dataCollection',
@@ -576,7 +479,7 @@ const route = createRoute({
576479
{
577480
label: 'TypeScript',
578481
language: 'typescript',
579-
filename: 'src/app/routes/api/sentry-example-api.ts',
482+
filename: 'src/routes/api/sentry-example.ts',
580483
code: `import { createFileRoute } from "@tanstack/react-router";
581484
import { json } from "@tanstack/react-start";
582485

0 commit comments

Comments
 (0)