Skip to content

Commit 1caf420

Browse files
authored
feat(nuxt): Wrap config in nuxt context (#13457)
To be able to use `useRuntimeConfig()` in the user-defined Sentry config file, it needs to be wrapped in a Nuxt context. Right now, this is only done on the client-side because the server-side is currently still in the `public` folder (this will be changed) and cannot use the virtual `#imports` import (`useRuntimeConfig` is imported from `#imports`)
1 parent c24f6d0 commit 1caf420

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

dev-packages/e2e-tests/test-applications/nuxt-3/nuxt.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ export default defineNuxtConfig({
44
imports: {
55
autoImport: false,
66
},
7+
runtimeConfig: {
8+
public: {
9+
sentry: {
10+
dsn: 'https://[email protected]/1337',
11+
},
12+
},
13+
},
714
});
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import * as Sentry from '@sentry/nuxt';
2+
import { useRuntimeConfig } from '#imports';
23

34
Sentry.init({
45
environment: 'qa', // dynamic sampling bias to keep transactions
5-
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
dsn: useRuntimeConfig().public.sentry.dsn,
67
tunnel: `http://localhost:3031/`, // proxy server
78
tracesSampleRate: 1.0,
89
});

packages/nuxt/src/module.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,18 @@ export default defineNuxtModule<ModuleOptions>({
2626
addPluginTemplate({
2727
mode: 'client',
2828
filename: 'sentry-client-config.mjs',
29-
getContents: () =>
30-
`import "${buildDirResolver.resolve(`/${clientConfigFile}`)}"\n` +
31-
'import { defineNuxtPlugin } from "#imports"\n' +
32-
'export default defineNuxtPlugin(() => {})',
29+
30+
// Dynamic import of config file to wrap it within a Nuxt context (here: defineNuxtPlugin)
31+
// Makes it possible to call useRuntimeConfig() in the user-defined sentry config file
32+
getContents: () => `
33+
import { defineNuxtPlugin } from "#imports";
34+
35+
export default defineNuxtPlugin({
36+
name: 'sentry-client-config',
37+
async setup() {
38+
await import("${buildDirResolver.resolve(`/${clientConfigFile}`)}")
39+
}
40+
});`,
3341
});
3442

3543
addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/sentry.client'), mode: 'client' });

packages/nuxt/src/runtime/plugins/sentry.client.ts

+20-16
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,28 @@ interface VueRouter {
2929
// Tree-shakable guard to remove all code related to tracing
3030
declare const __SENTRY_TRACING__: boolean;
3131

32-
export default defineNuxtPlugin(nuxtApp => {
33-
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside
34-
// will get tree-shaken away
35-
if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {
36-
const sentryClient = getClient();
32+
export default defineNuxtPlugin({
33+
name: 'sentry-client-integrations',
34+
dependsOn: ['sentry-client-config'],
35+
async setup(nuxtApp) {
36+
// This evaluates to true unless __SENTRY_TRACING__ is text-replaced with "false", in which case everything inside
37+
// will get tree-shaken away
38+
if (typeof __SENTRY_TRACING__ === 'undefined' || __SENTRY_TRACING__) {
39+
const sentryClient = getClient();
3740

38-
if (sentryClient && '$router' in nuxtApp) {
39-
sentryClient.addIntegration(
40-
browserTracingIntegration({ router: nuxtApp.$router as VueRouter, routeLabel: 'path' }),
41-
);
41+
if (sentryClient && '$router' in nuxtApp) {
42+
sentryClient.addIntegration(
43+
browserTracingIntegration({ router: nuxtApp.$router as VueRouter, routeLabel: 'path' }),
44+
);
45+
}
4246
}
43-
}
4447

45-
nuxtApp.hook('app:created', vueApp => {
46-
const sentryClient = getClient();
48+
nuxtApp.hook('app:created', vueApp => {
49+
const sentryClient = getClient();
4750

48-
if (sentryClient) {
49-
sentryClient.addIntegration(vueIntegration({ app: vueApp }));
50-
}
51-
});
51+
if (sentryClient) {
52+
sentryClient.addIntegration(vueIntegration({ app: vueApp }));
53+
}
54+
});
55+
},
5256
});

0 commit comments

Comments
 (0)