import * as Sentry from '@sentry/nextjs'; /** * 客户端 Sentry 初始化入口。原 sentry.client.config.ts 的内容整体迁移至此,原因(佐证来自已安装版本源码): * - Next 16 起 `next build` 默认使用 Turbopack * (node_modules/next/dist/docs/01-app/02-guides/upgrading/version-16.md:128), * 而 SDK 明确警告 sentry.client.config.ts 在 Turbopack 下不再被加载: * node_modules/@sentry/nextjs/build/esm/config/webpack.js:207-213 * ("renaming your sentry.client.config.ts file, or moving its content to instrumentation-client.ts") * - instrumentation-client.ts 是 Next 官方客户端入口约定:在 HTML 加载后、React hydration 前执行, * 且仅顶层同步代码保证在 hydration 前完成 —— 因此本文件顶层直接同步调用 Sentry.init: * node_modules/next/dist/docs/01-app/03-api-reference/03-file-conventions/instrumentation-client.md(Usage / Execution timing 两节) * - 路由导航插桩要求导出 onRouterTransitionStart = Sentry.captureRouterTransitionStart,否则构建期告警: * node_modules/@sentry/nextjs/build/esm/config/withSentryConfig/getFinalConfigObjectUtils.js:131-137; * captureRouterTransitionStart 见 node_modules/@sentry/nextjs/build/types/client/index.d.ts:8 * * 无 DSN 门禁与 GA(GoogleAnalytics.tsx 无 ID 即返回 null)同一约定:NEXT_PUBLIC_SENTRY_DSN 为空时 * 跳过 Sentry.init,SDK 保持惰性、不发任何请求。 */ const SENTRY_DSN = process.env.NEXT_PUBLIC_SENTRY_DSN || ''; if (SENTRY_DSN) { Sentry.init({ dsn: SENTRY_DSN, environment: process.env.NODE_ENV || 'development', enabled: process.env.NODE_ENV !== 'development', tracesSampleRate: process.env.NODE_ENV === 'production' ? 0.2 : 0, replaysSessionSampleRate: 0, replaysOnErrorSampleRate: 1.0, ignoreErrors: [ /_AutofillCallbackHandler/, /NetworkError/, /webkit\.messageHandlers/, /Non-Error promise rejection captured/, /^Loading CSS chunk.*failed/, /^Loading chunk.*failed/, /^Failed to fetch dynamically imported module/, ], denyUrls: [ /extensions\//i, /^chrome:\/\//i, /webappstoolbars/i, /safari-extension:/i, /mypersonalassistant/i, ], initialScope: { tags: { project: 'novalon-website', version: process.env.npm_package_version || 'unknown', deployment: process.env.NODE_ENV || 'unknown', }, user: { id: 'anonymous', segment: 'visitor', }, }, beforeSend(event) { if (event.request?.url) { event.request.url = event.request.url.replace(/\/[a-f0-9]{32}/gi, '/[hash]'); } if (event.breadcrumbs) { event.breadcrumbs = event.breadcrumbs.filter((breadcrumb) => { return !( breadcrumb.category === 'console' && breadcrumb.level === 'log' ); }); } return event; }, integrations: [ Sentry.browserTracingIntegration(), Sentry.replayIntegration({ maskAllText: true, blockAllMedia: true, }), ], }); console.log('[Sentry] Client-side monitoring initialized'); } else { console.warn('[Sentry] NEXT_PUBLIC_SENTRY_DSN not configured, error monitoring disabled'); } // App Router 导航插桩钩子(见文件头注释;仅声明函数引用,无副作用,DSN 缺失时 SDK 未 init 也不会发送) export const onRouterTransitionStart = Sentry.captureRouterTransitionStart;