Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions packages/vike-react-sentry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions packages/vike-react-sentry/src/integration/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ const config = {
declare global {
namespace Vike {
interface Config {
sentry?: SentryOptions | ((globalContext: GlobalContext) => SentryOptions)
sentry?: SentryOptions | (() => SentryOptions | Promise<SentryOptions>)
sentryVite?: SentryVitePluginOptions
}

interface ConfigResolved {
sentry?: (SentryOptions | ((globalContext: GlobalContext) => SentryOptions))[]
sentry?: (SentryOptions | (() => SentryOptions | Promise<SentryOptions>))[]
sentryVite?: SentryVitePluginOptions
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { resolveDsn } from '../utils/resolveDsn.js'
import { TRACE_DEFAULT_SAMPLE_RATE } from './constants.js'

async function onCreateGlobalContext(globalContext: GlobalContextClient): Promise<void> {
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { resolveDsn } from '../utils/resolveDsn.js'
import { TRACE_DEFAULT_SAMPLE_RATE } from './constants.js'

async function onCreateGlobalContext(globalContext: GlobalContextServer): Promise<void> {
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))
Expand Down