Skip to content

Commit 2af6b75

Browse files
nicohrubecclaude
andauthored
feat(tanstackstart-react): Enable component tracking (#21149)
Passes the `reactComponentAnnotation` option through to `sentryVitePlugin`, adding `data-sentry-component` and `data-sentry-source-file` attributes to React components at build time for better component names in Session Replay and breadcrumbs. Fixes #18288 --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fb83c9b commit 2af6b75

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

packages/tanstackstart-react/src/vite/sentryTanstackStart.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ export interface SentryTanstackStartOptions extends BuildTimeOptionsBase {
2222
*/
2323
autoInstrumentMiddleware?: boolean;
2424

25+
/**
26+
* Options related to react component name annotations.
27+
* Disabled by default, unless a value is set for this option.
28+
* When enabled, your app's DOM will automatically be annotated during build-time with their respective component names.
29+
* This will unlock the capability to search for Replays in Sentry by component name, as well as see component names in breadcrumbs and performance monitoring.
30+
* Please note that this feature is not currently supported by the esbuild bundler plugins, and will only annotate React components
31+
*/
32+
reactComponentAnnotation?: {
33+
/**
34+
* Whether the component name annotate plugin should be enabled or not.
35+
*/
36+
enabled?: boolean;
37+
38+
/**
39+
* A list of strings representing the names of components to ignore. The plugin will not apply `data-sentry` annotations on the DOM element for these components.
40+
*/
41+
ignoredComponents?: string[];
42+
};
43+
2544
/**
2645
* Configures a framework-managed same-origin tunnel route for Sentry envelopes.
2746
*

packages/tanstackstart-react/src/vite/sourceMaps.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import type { BuildTimeOptionsBase } from '@sentry/core';
21
import { sentryVitePlugin } from '@sentry/vite-plugin';
32
import type { Plugin, UserConfig } from 'vite';
3+
import type { SentryTanstackStartOptions } from './sentryTanstackStart';
44

55
type FilesToDeleteAfterUpload = string | string[] | undefined;
66

77
/**
88
* A Sentry plugin for adding the @sentry/vite-plugin to automatically upload source maps to Sentry.
99
*/
10-
export function makeAddSentryVitePlugin(options: BuildTimeOptionsBase): Plugin[] {
10+
export function makeAddSentryVitePlugin(options: SentryTanstackStartOptions): Plugin[] {
1111
const {
1212
applicationKey,
1313
authToken,
@@ -22,6 +22,7 @@ export function makeAddSentryVitePlugin(options: BuildTimeOptionsBase): Plugin[]
2222
silent,
2323
sourcemaps,
2424
telemetry,
25+
reactComponentAnnotation,
2526
} = options;
2627

2728
// defer resolving the filesToDeleteAfterUpload until we got access to the Vite config
@@ -72,6 +73,10 @@ export function makeAddSentryVitePlugin(options: BuildTimeOptionsBase): Plugin[]
7273
rewriteSources: (sourcemaps as unknown as { rewriteSources?: unknown } | undefined)?.rewriteSources as never,
7374
filesToDeleteAfterUpload: filesToDeleteAfterUploadPromise,
7475
},
76+
reactComponentAnnotation: {
77+
enabled: reactComponentAnnotation?.enabled ?? undefined,
78+
ignoredComponents: reactComponentAnnotation?.ignoredComponents ?? undefined,
79+
},
7580
telemetry: telemetry ?? true,
7681
url: sentryUrl,
7782
_metaOptions: {
@@ -87,7 +92,7 @@ export function makeAddSentryVitePlugin(options: BuildTimeOptionsBase): Plugin[]
8792
/**
8893
* A Sentry plugin for TanStack Start React to enable "hidden" source maps if they are unset.
8994
*/
90-
export function makeEnableSourceMapsVitePlugin(options: BuildTimeOptionsBase): Plugin[] {
95+
export function makeEnableSourceMapsVitePlugin(options: SentryTanstackStartOptions): Plugin[] {
9196
return [
9297
{
9398
name: 'sentry-tanstackstart-react-source-maps',
@@ -123,7 +128,7 @@ export function makeEnableSourceMapsVitePlugin(options: BuildTimeOptionsBase): P
123128
*/
124129
export function getUpdatedSourceMapSettings(
125130
viteConfig: UserConfig,
126-
sentryPluginOptions?: BuildTimeOptionsBase,
131+
sentryPluginOptions?: SentryTanstackStartOptions,
127132
): boolean | 'inline' | 'hidden' {
128133
viteConfig.build = viteConfig.build || {};
129134

packages/tanstackstart-react/test/vite/sourceMaps.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,37 @@ describe('makeAddSentryVitePlugin()', () => {
242242
}),
243243
);
244244
});
245+
246+
it('passes reactComponentAnnotation options to the vite plugin', () => {
247+
makeAddSentryVitePlugin({
248+
reactComponentAnnotation: {
249+
enabled: true,
250+
ignoredComponents: ['MyIgnoredComponent'],
251+
},
252+
});
253+
254+
expect(sentryVitePluginSpy).toHaveBeenCalledWith(
255+
expect.objectContaining({
256+
reactComponentAnnotation: {
257+
enabled: true,
258+
ignoredComponents: ['MyIgnoredComponent'],
259+
},
260+
}),
261+
);
262+
});
263+
264+
it('passes undefined reactComponentAnnotation values when not configured', () => {
265+
makeAddSentryVitePlugin({});
266+
267+
expect(sentryVitePluginSpy).toHaveBeenCalledWith(
268+
expect.objectContaining({
269+
reactComponentAnnotation: {
270+
enabled: undefined,
271+
ignoredComponents: undefined,
272+
},
273+
}),
274+
);
275+
});
245276
});
246277

247278
describe('getUpdatedSourceMapSettings', () => {

0 commit comments

Comments
 (0)