Skip to content

Commit 556f99f

Browse files
nicohrubecclaude
andauthored
test(tanstackstart-react): Move initialization to client entry point (#21161)
Moves `Sentry.init()` from `getRouter()` to the client entry point so it runs before hydration in our e2e test applications. Previously, any errors or breadcrumbs from before hydration (e.g. third-party library inits) were silently lost because `Sentry.init()` only ran during hydration inside `getRouter()`. Fixes #21088 --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ba97988 commit 556f99f

9 files changed

Lines changed: 139 additions & 18 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott
66

7+
- **test(tanstackstart-react): Move initialization to client entry point ([#21161](https://github.com/getsentry/sentry-javascript/pull/21161))**
8+
9+
Change the recommended setup for the SDK to do `Sentry.init()` in the client entry file to capture telemetry that is emitted ahead of page hydration.
10+
711
## 10.54.0
812

913
### Important Changes
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as Sentry from '@sentry/browser';
2+
import { StartClient } from '@tanstack/react-start/client';
3+
import { StrictMode, startTransition } from 'react';
4+
import { hydrateRoot } from 'react-dom/client';
5+
6+
Sentry.init({
7+
environment: 'qa',
8+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
9+
tracesSampleRate: 1.0,
10+
release: 'e2e-test',
11+
tunnel: 'http://localhost:3031/',
12+
});
13+
14+
console.log('early-breadcrumb-from-client-entry');
15+
16+
if (window.location.pathname === '/crash-before-hydration') {
17+
throw new Error('Client Entry Crash');
18+
}
19+
20+
startTransition(() => {
21+
hydrateRoot(
22+
document,
23+
<StrictMode>
24+
<StartClient />
25+
</StrictMode>,
26+
);
27+
});

‎dev-packages/e2e-tests/test-applications/tanstackstart-react-cloudflare/src/router.tsx‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@ export const getRouter = () => {
99
});
1010

1111
if (!router.isServer) {
12-
Sentry.init({
13-
environment: 'qa',
14-
dsn: 'https://public@dsn.ingest.sentry.io/1337',
15-
integrations: [Sentry.browserTracingIntegration()],
16-
tracesSampleRate: 1.0,
17-
release: 'e2e-test',
18-
tunnel: 'http://localhost:3031/',
19-
});
12+
Sentry.addIntegration(Sentry.browserTracingIntegration());
2013
}
2114

2215
return router;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createFileRoute } from '@tanstack/react-router';
2+
3+
export const Route = createFileRoute('/crash-before-hydration')({
4+
component: CrashPage,
5+
});
6+
7+
function CrashPage() {
8+
return (
9+
<div>
10+
<p>This page crashes in client.tsx before hydration</p>
11+
</div>
12+
);
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForError } from '@sentry-internal/test-utils';
3+
4+
test('should capture errors thrown in client entry before hydration', async ({ page }) => {
5+
const errorEventPromise = waitForError('tanstackstart-react-cloudflare', errorEvent => {
6+
return errorEvent?.exception?.values?.[0]?.value === 'Client Entry Crash';
7+
});
8+
9+
await page.goto('/crash-before-hydration');
10+
11+
const errorEvent = await errorEventPromise;
12+
13+
expect(errorEvent.exception?.values?.[0]).toMatchObject({
14+
type: 'Error',
15+
value: 'Client Entry Crash',
16+
});
17+
18+
const consoleBreadcrumbs = errorEvent.breadcrumbs?.filter(
19+
b => b.category === 'console' && b.message?.includes('early-breadcrumb-from-client-entry'),
20+
);
21+
22+
expect(consoleBreadcrumbs?.length).toBeGreaterThanOrEqual(1);
23+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as Sentry from '@sentry/tanstackstart-react';
2+
import { StartClient } from '@tanstack/react-start/client';
3+
import { StrictMode, startTransition } from 'react';
4+
import { hydrateRoot } from 'react-dom/client';
5+
6+
Sentry.init({
7+
environment: 'qa', // dynamic sampling bias to keep transactions
8+
dsn: __APP_DSN__,
9+
// We recommend adjusting this value in production, or using tracesSampler
10+
// for finer control
11+
tracesSampleRate: 1.0,
12+
release: 'e2e-test',
13+
tunnel: __APP_TUNNEL__,
14+
});
15+
16+
console.log('early-breadcrumb-from-client-entry');
17+
18+
if (window.location.pathname === '/crash-before-hydration') {
19+
throw new Error('Client Entry Crash');
20+
}
21+
22+
startTransition(() => {
23+
hydrateRoot(
24+
document,
25+
<StrictMode>
26+
<StartClient />
27+
</StrictMode>,
28+
);
29+
});

‎dev-packages/e2e-tests/test-applications/tanstackstart-react/src/router.tsx‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,7 @@ export const getRouter = () => {
99
});
1010

1111
if (!router.isServer) {
12-
Sentry.init({
13-
environment: 'qa', // dynamic sampling bias to keep transactions
14-
dsn: __APP_DSN__,
15-
integrations: [Sentry.tanstackRouterBrowserTracingIntegration(router)],
16-
// We recommend adjusting this value in production, or using tracesSampler
17-
// for finer control
18-
tracesSampleRate: 1.0,
19-
release: 'e2e-test',
20-
tunnel: __APP_TUNNEL__,
21-
});
12+
Sentry.addIntegration(Sentry.tanstackRouterBrowserTracingIntegration(router));
2213
}
2314

2415
return router;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { createFileRoute } from '@tanstack/react-router';
2+
3+
export const Route = createFileRoute('/crash-before-hydration')({
4+
component: CrashPage,
5+
});
6+
7+
function CrashPage() {
8+
return (
9+
<div>
10+
<p>This page crashes in client.tsx before hydration</p>
11+
</div>
12+
);
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect, test } from '@playwright/test';
2+
import { waitForError } from '@sentry-internal/test-utils';
3+
4+
const usesManagedTunnelRoute =
5+
(process.env.E2E_TEST_TUNNEL_ROUTE_MODE ?? 'off') !== 'off' || process.env.E2E_TEST_CUSTOM_TUNNEL_ROUTE === '1';
6+
7+
test.skip(usesManagedTunnelRoute, 'Default e2e suites run only in the proxy variant');
8+
9+
test('should capture errors thrown in client entry before hydration', async ({ page }) => {
10+
const errorEventPromise = waitForError('tanstackstart-react', errorEvent => {
11+
return errorEvent?.exception?.values?.[0]?.value === 'Client Entry Crash';
12+
});
13+
14+
await page.goto('/crash-before-hydration');
15+
16+
const errorEvent = await errorEventPromise;
17+
18+
expect(errorEvent.exception?.values?.[0]).toMatchObject({
19+
type: 'Error',
20+
value: 'Client Entry Crash',
21+
});
22+
23+
const consoleBreadcrumbs = errorEvent.breadcrumbs?.filter(
24+
b => b.category === 'console' && b.message?.includes('early-breadcrumb-from-client-entry'),
25+
);
26+
27+
expect(consoleBreadcrumbs?.length).toBeGreaterThanOrEqual(1);
28+
});

0 commit comments

Comments
 (0)