-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add a tunnel route helper 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
Open
nikolovlazar
wants to merge
7
commits into
develop
Choose a base branch
from
lazarnikolov/js-2140-tanstack-start-tunnel-adapter
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+748
−12
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef7b5e9
Add a TanStack Start tunnel route helper
nikolovlazar 1691509
Merge branch 'develop' into lazarnikolov/js-2140-tanstack-start-tunne…
nikolovlazar 316be89
Add TanStack Start tunnel route e2e coverage
nikolovlazar e6b6290
Merge branch 'lazarnikolov/js-2140-tanstack-start-tunnel-adapter' of …
nikolovlazar 08b4154
Refactor TanStack Start e2e app to cover managed tunnel route variants
nikolovlazar 3991017
Merge branch 'develop' into lazarnikolov/js-2140-tanstack-start-tunne…
nikolovlazar 98202a9
Generate stable 8-character tunnel route paths
nikolovlazar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
dev-packages/e2e-tests/test-applications/tanstackstart-react/src/globals.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
dev-packages/e2e-tests/test-applications/tanstackstart-react/src/routes/custom-monitor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| 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], | ||
| }), | ||
| }); |
5 changes: 5 additions & 0 deletions
5
dev-packages/e2e-tests/test-applications/tanstackstart-react/tests/errors.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
dev-packages/e2e-tests/test-applications/tanstackstart-react/tests/middleware.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
dev-packages/e2e-tests/test-applications/tanstackstart-react/tests/transaction.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
dev-packages/e2e-tests/test-applications/tanstackstart-react/tests/tunnel.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| 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'); | ||
|
|
||
| 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'; | ||
| }); | ||
|
|
||
| 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 (tunnelRouteMode === 'static') { | ||
| expect(managedTunnelUrl.pathname).toBe('/monitor'); | ||
| } else if (tunnelRouteMode === 'custom') { | ||
| expect(managedTunnelUrl.pathname).toBe('/custom-monitor'); | ||
| } else { | ||
| expect(managedTunnelUrl.pathname).toMatch(/^\/[a-z0-9]{8}$/); | ||
| expect(managedTunnelUrl.pathname).not.toBe('/monitor'); | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| expect(errorEvent.exception?.values?.[0]?.value).toBe('Sentry Client Test Error'); | ||
| expect(errorEvent.transaction).toBe('/'); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { 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 }) => { | ||
| return handleTunnelRequest({ | ||
| request, | ||
| allowedDsns: options.allowedDsns, | ||
| }); | ||
| }, | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overly broad POST matcher risks test flakiness
Low Severity
The
page.waitForResponsefilter matches any POST request to the same origin, not specifically the tunnel endpoint. Other same-origin POST requests (e.g., pageload transaction envelopes, server function calls) could satisfy the matcher first, causing the test to assert against the wrong response. Narrowing the filter (e.g., matching on the expected tunnel pathname) would make it more robust.Triggered by project rule: PR Review Guidelines for Cursor Bot
Reviewed by Cursor Bugbot for commit 98202a9. Configure here.