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
feat(tanstackstart-react): Auto-instrument global middleware#18844
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
704a7816cc5784b4a0d83b6ddbff8c6f0fa207a96a53e9099b9c51e96eb1c8b4dcdabf9aaabd9bfa238a086b9f605e08f1bc0acdd8e60de89bbd0b0283a5451e736b4841a3119ad7a8d286f624505c92c07d5e77073e3528ce535876c746cbbf7be463a6660c99e1a1e2bf4b0b7d35adc6d76248142b20463d7f01ed23082706bc0bdb9ac5a39e88f00fbbbd993ba3faa7adc565a1a3e7137d00b5ed7e2b0c6045e0c1428f4d73e0d2f080150f8db04f6cc68a296b012460d54File 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 |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| import { createStart } from '@tanstack/react-start'; | ||
| import { wrappedGlobalRequestMiddleware, wrappedGlobalFunctionMiddleware } from './middleware'; | ||
| // NOTE: These are NOT wrapped - auto-instrumentation via the Vite plugin will wrap them | ||
| import { globalRequestMiddleware, globalFunctionMiddleware } from './middleware'; | ||
| export const startInstance = createStart(() => { | ||
| return { | ||
| requestMiddleware: [wrappedGlobalRequestMiddleware], | ||
| functionMiddleware: [wrappedGlobalFunctionMiddleware], | ||
| requestMiddleware: [globalRequestMiddleware], | ||
| functionMiddleware: [globalFunctionMiddleware], | ||
| }; | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import type { Plugin } from 'vite'; | ||
| type AutoInstrumentMiddlewareOptions = { | ||
| enabled?: boolean; | ||
| debug?: boolean; | ||
| }; | ||
| /** | ||
| * A Vite plugin that automatically instruments TanStack Start middlewares | ||
| * by wrapping `requestMiddleware` and `functionMiddleware` arrays in `createStart()`. | ||
| */ | ||
| export function makeAutoInstrumentMiddlewarePlugin(options: AutoInstrumentMiddlewareOptions = {}): Plugin { | ||
| const { enabled = true, debug = false } = options; | ||
| return { | ||
| name: 'sentry-tanstack-middleware-auto-instrument', | ||
| enforce: 'pre', | ||
| transform(code, id) { | ||
| if (!enabled) { | ||
| return null; | ||
| } | ||
| // Skip if not a TS/JS file | ||
| if (!/\.(ts|tsx|js|jsx|mjs|mts)$/.test(id)) { | ||
| return null; | ||
| } | ||
| // Only wrap requestMiddleware and functionMiddleware in createStart() | ||
| // createStart() should always be in a file named start.ts | ||
| if (!id.includes('start') || !code.includes('createStart(')) { | ||
| return null; | ||
| } | ||
| // Skip if the user already did some manual wrapping | ||
| if (code.includes('wrapMiddlewaresWithSentry')) { | ||
| return null; | ||
| } | ||
| let transformed = code; | ||
| let needsImport = false; | ||
| const skippedMiddlewares: string[] = []; | ||
| transformed = transformed.replace( | ||
| /(requestMiddleware|functionMiddleware)\s*:\s*\[([^\]]*)\]/g, | ||
| (match: string, key: string, contents: string) => { | ||
| const objContents = arrayToObjectShorthand(contents); | ||
| if (objContents) { | ||
| needsImport = true; | ||
| if (debug) { | ||
| // eslint-disable-next-line no-console | ||
| console.log(`[Sentry] Auto-wrapping ${key} in ${id}`); | ||
| } | ||
| return `${key}: wrapMiddlewaresWithSentry(${objContents})`; | ||
| } | ||
| // Track middlewares that couldn't be auto-wrapped | ||
| // Skip if we matched whitespace only | ||
| if (contents.trim()) { | ||
Member 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. Q: why do we trim the contents here? To make sure it's not an empty file? Just curious MemberAuthor 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. Just in case people have weird formatting and we match whitespace only when trying to match the middlewares, unlikely case but shouldn't hurt to have it | ||
| skippedMiddlewares.push(key); | ||
| } | ||
| return match; | ||
| }, | ||
| ); | ||
cursor[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. nicohrubec marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // Warn about middlewares that couldn't be auto-wrapped | ||
| if (skippedMiddlewares.length > 0) { | ||
| // eslint-disable-next-line no-console | ||
| console.warn( | ||
| `[Sentry] Could not auto-instrument ${skippedMiddlewares.join(' and ')} in ${id}. ` + | ||
| 'To instrument these middlewares, use wrapMiddlewaresWithSentry() manually. ', | ||
| ); | ||
| } | ||
| // We didn't wrap any middlewares, so we don't need to import the wrapMiddlewaresWithSentry function | ||
| if (!needsImport) { | ||
| return null; | ||
| } | ||
| const sentryImport = "import { wrapMiddlewaresWithSentry } from '@sentry/tanstackstart-react';\n"; | ||
| // Check for 'use server' or 'use client' directives, these need to be before any imports | ||
| const directiveMatch = transformed.match(/^(['"])use (client|server)\1;?\s*\n?/); | ||
| if (directiveMatch) { | ||
| // Insert import after the directive | ||
| const directive = directiveMatch[0]; | ||
| transformed = directive + sentryImport + transformed.slice(directive.length); | ||
| } else { | ||
| transformed = sentryImport + transformed; | ||
| } | ||
| return { code: transformed, map: null }; | ||
| }, | ||
| }; | ||
| } | ||
| /** | ||
| * Convert array contents to object shorthand syntax. | ||
| * e.g., "foo, bar, baz" → "{ foo, bar, baz }" | ||
| * | ||
| * Returns null if contents contain non-identifier expressions (function calls, etc.) | ||
| * which cannot be converted to object shorthand. | ||
| */ | ||
| export function arrayToObjectShorthand(contents: string): string | null { | ||
| const items = contents | ||
| .split(',') | ||
| .map(s => s.trim()) | ||
| .filter(Boolean); | ||
| // Only convert if all items are valid identifiers (no complex expressions) | ||
| const allIdentifiers = items.every(item => /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(item)); | ||
| if (!allIdentifiers || items.length === 0) { | ||
| return null; | ||
| } | ||
| // Deduplicate to avoid invalid syntax like { foo, foo } | ||
| const uniqueItems = [...new Set(items)]; | ||
| return `{ ${uniqueItems.join(', ')} }`; | ||
| } | ||
nicohrubec marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { sentryTanstackStart } from './sentryTanstackStart'; | ||
| export type { SentryTanstackStartOptions } from './sentryTanstackStart'; |
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.