Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Tunnel route helper + Dynamic tunnel route generator for TanStack Start React#20264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ef7b5e91691509316be89e6b629008b4154399101798202a9320a55f73fbbeb5a2c6e9ee3da8d1d827f57c9124c4390705e68a210c99361652c33cbd77dec429a5d89c356b379e04b81da0365100c9a99af60d11File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| declare const __APP_DSN__: string; | ||
| declare const __APP_TUNNEL__: string | undefined; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import * as Sentry from '@sentry/tanstackstart-react'; | ||
| import { createFileRoute } from '@tanstack/react-router'; | ||
| const USE_CUSTOM_TUNNEL_ROUTE = process.env.E2E_TEST_CUSTOM_TUNNEL_ROUTE === '1'; | ||
| const DEFAULT_DSN = 'https://public@dsn.ingest.sentry.io/1337'; | ||
| const TUNNEL_DSN = 'http://public@localhost:3031/1337'; | ||
| // Example of a manually defined tunnel endpoint without relying on the | ||
| // managed route injected by `sentryTanstackStart({ tunnelRoute: ... })`. | ||
| // If you use a custom route like this one, set `tunnel: '/custom-monitor'` in the client SDK's | ||
| // `Sentry.init()` call so browser events are sent to the same endpoint. | ||
| export const Route = createFileRoute('/custom-monitor')({ | ||
| server: Sentry.createSentryTunnelRoute({ | ||
| allowedDsns: [USE_CUSTOM_TUNNEL_ROUTE ? TUNNEL_DSN : DEFAULT_DSN], | ||
| }), | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForError } from '@sentry-internal/test-utils'; | ||
| const tunnelRouteMode = | ||
| process.env.E2E_TEST_TUNNEL_ROUTE_MODE ?? (process.env.E2E_TEST_CUSTOM_TUNNEL_ROUTE === '1' ? 'custom' : 'off'); | ||
| const expectedTunnelPathMatcher = | ||
| tunnelRouteMode === 'static' | ||
| ? '/monitor' | ||
| : tunnelRouteMode === 'custom' | ||
| ? '/custom-monitor' | ||
| : tunnelRouteMode === 'object' | ||
| ? '/object-monitor' | ||
| : /^\/[a-z0-9]{8}$/; | ||
| test.skip(tunnelRouteMode === 'off', 'Tunnel assertions only run in the tunnel-route variants'); | ||
| test('Sends client-side errors through the configured tunnel route', async ({ page }) => { | ||
| const errorEventPromise = waitForError('tanstackstart-react', errorEvent => { | ||
| return errorEvent?.exception?.values?.[0]?.value === 'Sentry Client Test Error'; | ||
| }); | ||
| await page.goto('/'); | ||
| const pageOrigin = new URL(page.url()).origin; | ||
| await expect(page.locator('button').filter({ hasText: 'Break the client' })).toBeVisible(); | ||
| const managedTunnelResponsePromise = page.waitForResponse(response => { | ||
| const responseUrl = new URL(response.url()); | ||
| return ( | ||
| responseUrl.origin === pageOrigin && | ||
| response.request().method() === 'POST' && | ||
| (typeof expectedTunnelPathMatcher === 'string' | ||
| ? responseUrl.pathname === expectedTunnelPathMatcher | ||
| : expectedTunnelPathMatcher.test(responseUrl.pathname)) | ||
| ); | ||
| }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tunnel test may capture unrelated Sentry requestLow Severity The Triggered by project rule: PR Review Guidelines for Cursor Bot Reviewed by Cursor Bugbot for commit 9e04b81. Configure here. | ||
| await page.locator('button').filter({ hasText: 'Break the client' }).click(); | ||
| const managedTunnelResponse = await managedTunnelResponsePromise; | ||
| const managedTunnelUrl = new URL(managedTunnelResponse.url()); | ||
| const errorEvent = await errorEventPromise; | ||
| expect(managedTunnelResponse.status()).toBe(200); | ||
| expect(managedTunnelUrl.origin).toBe(pageOrigin); | ||
| if (typeof expectedTunnelPathMatcher === 'string') { | ||
| expect(managedTunnelUrl.pathname).toBe(expectedTunnelPathMatcher); | ||
| } else { | ||
| expect(managedTunnelUrl.pathname).toMatch(expectedTunnelPathMatcher); | ||
| expect(managedTunnelUrl.pathname).not.toBe('/monitor'); | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('Sentry Client Test Error'); | ||
| expect(errorEvent.transaction).toBe('/'); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { consoleSandbox } from '@sentry/core'; | ||
| import type { BrowserOptions as ReactBrowserOptions } from '@sentry/react'; | ||
| declare const __SENTRY_TANSTACKSTART_TUNNEL_ROUTE__: string | undefined; | ||
| let hasWarnedAboutManagedTunnelRouteOverride = false; | ||
| /** | ||
| * Applies the managed tunnel route from `sentryTanstackStart({ tunnelRoute: ... })` unless the user already | ||
| * configured an explicit runtime `tunnel` option in `Sentry.init()`. | ||
| */ | ||
| export function applyTunnelRouteOption(options: ReactBrowserOptions): void { | ||
| const managedTunnelRoute = | ||
| typeof __SENTRY_TANSTACKSTART_TUNNEL_ROUTE__ !== 'undefined' ? __SENTRY_TANSTACKSTART_TUNNEL_ROUTE__ : undefined; | ||
| if (!managedTunnelRoute) { | ||
| return; | ||
| } | ||
| if (options.tunnel) { | ||
| if (!hasWarnedAboutManagedTunnelRouteOverride) { | ||
| hasWarnedAboutManagedTunnelRouteOverride = true; | ||
| consoleSandbox(() => { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| '[@sentry/tanstackstart-react] `Sentry.init({ tunnel: ... })` overrides the managed `sentryTanstackStart({ tunnelRoute: ... })` route. Remove the runtime `tunnel` option if you want the managed tunnel route to be used.', | ||
| ); | ||
| }); | ||
| } | ||
| return; | ||
| } | ||
| options.tunnel = managedTunnelRoute; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -9,6 +9,7 @@ export { init } from './sdk'; | ||
| export { wrapFetchWithSentry } from './wrapFetchWithSentry'; | ||
| export { wrapMiddlewaresWithSentry } from './middleware'; | ||
| export { sentryGlobalRequestMiddleware, sentryGlobalFunctionMiddleware } from './globalMiddleware'; | ||
| export { createSentryTunnelRoute } from './tunnelRoute'; | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * A no-op stub of the browser tracing integration for the server. Router setup code is shared between client and server, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { dsnToString, getClient, handleTunnelRequest } from '@sentry/core'; | ||
| export interface CreateSentryTunnelRouteOptions { | ||
| allowedDsns?: string[]; | ||
| } | ||
| type SentryTunnelRouteHandlerContext = { | ||
| request: Request; | ||
| }; | ||
| type SentryTunnelRoute = { | ||
| handlers: { | ||
| POST: (context: SentryTunnelRouteHandlerContext) => Promise<Response>; | ||
| }; | ||
| }; | ||
| /** | ||
| * Creates a TanStack Start server route configuration for tunneling Sentry envelopes. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * import { createFileRoute } from '@tanstack/react-router'; | ||
| * import * as Sentry from '@sentry/tanstackstart-react'; | ||
| * | ||
| * export const Route = createFileRoute('/monitoring')({ | ||
| * server: Sentry.createSentryTunnelRoute({ | ||
| * allowedDsns: ['https://public@o0.ingest.sentry.io/0'], | ||
| * }), | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export function createSentryTunnelRoute(options: CreateSentryTunnelRouteOptions): SentryTunnelRoute { | ||
| return { | ||
| handlers: { | ||
| POST: async ({ request }) => { | ||
| const allowedDsnsFromOptions = options.allowedDsns?.length ? options.allowedDsns : undefined; | ||
| const allowedDsns = | ||
| allowedDsnsFromOptions ?? | ||
| (() => { | ||
| const client = getClient(); | ||
| const dsn = client?.getDsn(); | ||
| return dsn ? [dsnToString(dsn)] : undefined; | ||
| })(); | ||
| if (!allowedDsns) { | ||
| return new Response( | ||
| 'Tunnel route requires Sentry server SDK initialized with a DSN, or pass allowedDsns explicitly.', | ||
| { status: 500 }, | ||
| ); | ||
| } | ||
| return handleTunnelRequest({ | ||
| request, | ||
| allowedDsns, | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export { sentryTanstackStart } from './sentryTanstackStart'; | ||
| export type { SentryTanstackStartOptions } from './sentryTanstackStart'; | ||
| export type { TunnelRouteOptions } from './tunnelRoute'; |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.


Uh oh!
There was an error while loading. Please reload this page.