diff --git a/packages/vike-react-sentry/README.md b/packages/vike-react-sentry/README.md index 24fe9dfd..ff48c63a 100644 --- a/packages/vike-react-sentry/README.md +++ b/packages/vike-react-sentry/README.md @@ -77,43 +77,62 @@ See [examples/sentry](https://github.com/vikejs/vike-react/tree/main/examples/se Sentry SDK configuration options. +**Example 1: Using `getGlobalContext()` for dynamic configuration** + ```js -// pages/+sentry.js -// Environment: client, server +// pages/+sentry.server.js +// Environment: server -// Shared configuration (client & server) +import { getGlobalContext } from 'vike/server' -export default (globalContext) => ({ - tracesSampleRate: 1.0, // Capture 100% of transactions for tracing - debug: true, // Enable debug mode during development - environment: globalContext.isProduction ? 'production' : 'development', -}) +export default async () => { + const globalContext = await getGlobalContext() + return { + tracesSampleRate: 1.0, // Capture 100% of transactions for tracing + debug: true, // Enable debug mode during development + environment: globalContext.isProduction ? 'production' : 'development', + } +} ``` ```js // pages/+sentry.client.js // Environment: client -// Client-only configuration +import { getGlobalContext } from 'vike/client' + +export default async () => { + const globalContext = await getGlobalContext() + return { + tracesSampleRate: 1.0, + debug: true, + environment: globalContext.isProduction ? 'production' : 'development', + } +} +``` + +**Example 2: Static configuration** -export default (globalContext) => ({ +```js +// pages/+sentry.client.js +// Environment: client + +export default { integrations: [ // Add custom browser integrations here ], -}) +} ``` ```js // pages/+sentry.server.js // Environment: server -// Server-only configuration - -export default (globalContext) => ({ +export default { integrations: [ // Add custom Node.js integrations here ], -}) +} ``` > [!NOTE] diff --git a/packages/vike-react-sentry/src/integration/+config.ts b/packages/vike-react-sentry/src/integration/+config.ts index 2badb928..e61add91 100644 --- a/packages/vike-react-sentry/src/integration/+config.ts +++ b/packages/vike-react-sentry/src/integration/+config.ts @@ -48,12 +48,12 @@ const config = { declare global { namespace Vike { interface Config { - sentry?: SentryOptions | ((globalContext: GlobalContext) => SentryOptions) + sentry?: SentryOptions | (() => SentryOptions | Promise) sentryVite?: SentryVitePluginOptions } interface ConfigResolved { - sentry?: (SentryOptions | ((globalContext: GlobalContext) => SentryOptions))[] + sentry?: (SentryOptions | (() => SentryOptions | Promise))[] sentryVite?: SentryVitePluginOptions } } diff --git a/packages/vike-react-sentry/src/integration/onCreateGlobalContext.client.ts b/packages/vike-react-sentry/src/integration/onCreateGlobalContext.client.ts index d90d2c10..f4530fbb 100644 --- a/packages/vike-react-sentry/src/integration/onCreateGlobalContext.client.ts +++ b/packages/vike-react-sentry/src/integration/onCreateGlobalContext.client.ts @@ -8,12 +8,13 @@ import { resolveDsn } from '../utils/resolveDsn.js' import { TRACE_DEFAULT_SAMPLE_RATE } from './constants.js' async function onCreateGlobalContext(globalContext: GlobalContextClient): Promise { - const clientConfig = (globalContext.config.sentry || []).reverse().reduce((acc, curr) => { - if (typeof curr === 'function') { - curr = curr(globalContext) - } - return { ...acc, ...curr } - }, {}) as SentryOptions + const sentryConfigs = globalContext.config.sentry || [] + + const clientConfig: SentryOptions = {} + for (const curr of [...sentryConfigs].reverse()) { + const resolvedConfig = typeof curr === 'function' ? await curr() : curr + Object.assign(clientConfig, resolvedConfig) + } if (!SentryReact.getClient()) { SentryReact.init(resolveSentryClientSettings(clientConfig)) diff --git a/packages/vike-react-sentry/src/integration/onCreateGlobalContext.server.ts b/packages/vike-react-sentry/src/integration/onCreateGlobalContext.server.ts index 851d982d..e8d14c92 100644 --- a/packages/vike-react-sentry/src/integration/onCreateGlobalContext.server.ts +++ b/packages/vike-react-sentry/src/integration/onCreateGlobalContext.server.ts @@ -8,12 +8,13 @@ import { resolveDsn } from '../utils/resolveDsn.js' import { TRACE_DEFAULT_SAMPLE_RATE } from './constants.js' async function onCreateGlobalContext(globalContext: GlobalContextServer): Promise { - const serverConfig = (globalContext.config.sentry || []).reverse().reduce((acc, curr) => { - if (typeof curr === 'function') { - curr = curr(globalContext) - } - return { ...acc, ...curr } - }, {}) as SentryOptions + const sentryConfigs = globalContext.config.sentry || [] + + const serverConfig: SentryOptions = {} + for (const curr of [...sentryConfigs].reverse()) { + const resolvedConfig = typeof curr === 'function' ? await curr() : curr + Object.assign(serverConfig, resolvedConfig) + } if (!SentryNode.getClient()) { SentryNode.init(resolveSentryServerSettings(serverConfig))