Skip to content

Commit 84b81f6

Browse files
authored
Make +sentry config async with getGlobalContext() (#212)
1 parent b1ff613 commit 84b81f6

4 files changed

Lines changed: 50 additions & 29 deletions

File tree

packages/vike-react-sentry/README.md

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,43 +77,62 @@ See [examples/sentry](https://github.com/vikejs/vike-react/tree/main/examples/se
7777

7878
Sentry SDK configuration options.
7979

80+
**Example 1: Using `getGlobalContext()` for dynamic configuration**
81+
8082
```js
81-
// pages/+sentry.js
82-
// Environment: client, server
83+
// pages/+sentry.server.js
84+
// Environment: server
8385

84-
// Shared configuration (client & server)
86+
import { getGlobalContext } from 'vike/server'
8587

86-
export default (globalContext) => ({
87-
tracesSampleRate: 1.0, // Capture 100% of transactions for tracing
88-
debug: true, // Enable debug mode during development
89-
environment: globalContext.isProduction ? 'production' : 'development',
90-
})
88+
export default async () => {
89+
const globalContext = await getGlobalContext()
90+
return {
91+
tracesSampleRate: 1.0, // Capture 100% of transactions for tracing
92+
debug: true, // Enable debug mode during development
93+
environment: globalContext.isProduction ? 'production' : 'development',
94+
}
95+
}
9196
```
9297

9398
```js
9499
// pages/+sentry.client.js
95100
// Environment: client
96101

97-
// Client-only configuration
102+
import { getGlobalContext } from 'vike/client'
103+
104+
export default async () => {
105+
const globalContext = await getGlobalContext()
106+
return {
107+
tracesSampleRate: 1.0,
108+
debug: true,
109+
environment: globalContext.isProduction ? 'production' : 'development',
110+
}
111+
}
112+
```
113+
114+
**Example 2: Static configuration**
98115

99-
export default (globalContext) => ({
116+
```js
117+
// pages/+sentry.client.js
118+
// Environment: client
119+
120+
export default {
100121
integrations: [
101122
// Add custom browser integrations here
102123
],
103-
})
124+
}
104125
```
105126

106127
```js
107128
// pages/+sentry.server.js
108129
// Environment: server
109130

110-
// Server-only configuration
111-
112-
export default (globalContext) => ({
131+
export default {
113132
integrations: [
114133
// Add custom Node.js integrations here
115134
],
116-
})
135+
}
117136
```
118137

119138
> [!NOTE]

packages/vike-react-sentry/src/integration/+config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ const config = {
4848
declare global {
4949
namespace Vike {
5050
interface Config {
51-
sentry?: SentryOptions | ((globalContext: GlobalContext) => SentryOptions)
51+
sentry?: SentryOptions | (() => SentryOptions | Promise<SentryOptions>)
5252
sentryVite?: SentryVitePluginOptions
5353
}
5454

5555
interface ConfigResolved {
56-
sentry?: (SentryOptions | ((globalContext: GlobalContext) => SentryOptions))[]
56+
sentry?: (SentryOptions | (() => SentryOptions | Promise<SentryOptions>))[]
5757
sentryVite?: SentryVitePluginOptions
5858
}
5959
}

packages/vike-react-sentry/src/integration/onCreateGlobalContext.client.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import { resolveDsn } from '../utils/resolveDsn.js'
88
import { TRACE_DEFAULT_SAMPLE_RATE } from './constants.js'
99

1010
async function onCreateGlobalContext(globalContext: GlobalContextClient): Promise<void> {
11-
const clientConfig = (globalContext.config.sentry || []).reverse().reduce((acc, curr) => {
12-
if (typeof curr === 'function') {
13-
curr = curr(globalContext)
14-
}
15-
return { ...acc, ...curr }
16-
}, {}) as SentryOptions
11+
const sentryConfigs = globalContext.config.sentry || []
12+
13+
const clientConfig: SentryOptions = {}
14+
for (const curr of [...sentryConfigs].reverse()) {
15+
const resolvedConfig = typeof curr === 'function' ? await curr() : curr
16+
Object.assign(clientConfig, resolvedConfig)
17+
}
1718

1819
if (!SentryReact.getClient()) {
1920
SentryReact.init(resolveSentryClientSettings(clientConfig))

packages/vike-react-sentry/src/integration/onCreateGlobalContext.server.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import { resolveDsn } from '../utils/resolveDsn.js'
88
import { TRACE_DEFAULT_SAMPLE_RATE } from './constants.js'
99

1010
async function onCreateGlobalContext(globalContext: GlobalContextServer): Promise<void> {
11-
const serverConfig = (globalContext.config.sentry || []).reverse().reduce((acc, curr) => {
12-
if (typeof curr === 'function') {
13-
curr = curr(globalContext)
14-
}
15-
return { ...acc, ...curr }
16-
}, {}) as SentryOptions
11+
const sentryConfigs = globalContext.config.sentry || []
12+
13+
const serverConfig: SentryOptions = {}
14+
for (const curr of [...sentryConfigs].reverse()) {
15+
const resolvedConfig = typeof curr === 'function' ? await curr() : curr
16+
Object.assign(serverConfig, resolvedConfig)
17+
}
1718

1819
if (!SentryNode.getClient()) {
1920
SentryNode.init(resolveSentryServerSettings(serverConfig))

0 commit comments

Comments
 (0)