|
| 1 | +import { GLOBAL_OBJ } from '@sentry/core'; |
| 2 | + |
| 3 | +const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & { |
| 4 | + _sentryRewritesTunnelPath?: string; |
| 5 | +}; |
| 6 | + |
| 7 | +/** |
| 8 | + * Middleware config type for Next.js |
| 9 | + */ |
| 10 | +type MiddlewareConfig = { |
| 11 | + [key: string]: unknown; |
| 12 | + matcher?: string | string[]; |
| 13 | +}; |
| 14 | + |
| 15 | +/** |
| 16 | + * Configures middleware/proxy settings with Sentry-specific adjustments. |
| 17 | + * Automatically excludes the Sentry tunnel route from the matcher to prevent interference. |
| 18 | + * |
| 19 | + * @example |
| 20 | + * ```ts |
| 21 | + * // middleware.ts (Next.js <16) |
| 22 | + * import { withSentryMiddlewareConfig } from '@sentry/nextjs'; |
| 23 | + * |
| 24 | + * export const config = withSentryMiddlewareConfig({ |
| 25 | + * matcher: ['/api/:path*', '/admin/:path*'], |
| 26 | + * }); |
| 27 | + * ``` |
| 28 | + * |
| 29 | + * @example |
| 30 | + * ```ts |
| 31 | + * // proxy.ts (Next.js 16+) |
| 32 | + * import { withSentryProxyConfig } from '@sentry/nextjs'; |
| 33 | + * |
| 34 | + * export const config = withSentryProxyConfig({ |
| 35 | + * matcher: ['/api/:path*', '/admin/:path*'], |
| 36 | + * }); |
| 37 | + * ``` |
| 38 | + * |
| 39 | + * @param config - Middleware/proxy configuration object |
| 40 | + * @returns Updated config with Sentry tunnel route excluded from matcher |
| 41 | + */ |
| 42 | +export function withSentryMiddlewareConfig(config: MiddlewareConfig): MiddlewareConfig { |
| 43 | + const tunnelPath = process.env._sentryRewritesTunnelPath || globalWithInjectedValues._sentryRewritesTunnelPath; |
| 44 | + |
| 45 | + // If no tunnel path or no matcher, return config as-is |
| 46 | + if (!tunnelPath || !config.matcher) { |
| 47 | + return config; |
| 48 | + } |
| 49 | + |
| 50 | + // Convert to array for easier handling |
| 51 | + const matchers = Array.isArray(config.matcher) ? config.matcher : [config.matcher]; |
| 52 | + |
| 53 | + // Add negated matcher for the tunnel route |
| 54 | + // This tells Next.js to NOT run middleware on the tunnel path |
| 55 | + const tunnelExclusion = `/((?!${tunnelPath.replace(/^\//, '')}).*)`; |
| 56 | + |
| 57 | + // Return updated config with tunnel exclusion |
| 58 | + return { |
| 59 | + ...config, |
| 60 | + matcher: [...matchers, tunnelExclusion], |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Alias for `withSentryMiddlewareConfig` to support Next.js 16+ terminology. |
| 66 | + * In Next.js 16+, middleware files are called "proxy" files. |
| 67 | + * |
| 68 | + * @example |
| 69 | + * ```ts |
| 70 | + * // proxy.ts (Next.js 16+) |
| 71 | + * import { withSentryProxyConfig } from '@sentry/nextjs'; |
| 72 | + * |
| 73 | + * export const config = withSentryProxyConfig({ |
| 74 | + * matcher: ['/api/:path*', '/admin/:path*'], |
| 75 | + * }); |
| 76 | + * ``` |
| 77 | + */ |
| 78 | +export const withSentryProxyConfig = withSentryMiddlewareConfig; |
0 commit comments