Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): Wrap config in nuxt context #13457

Merged
merged 6 commits into from
Aug 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ export default defineNuxtConfig({
imports: {
autoImport: false,
},
runtimeConfig: {
public: {
sentry: {
dsn: 'https://public@dsn.ingest.sentry.io/1337',
},
},
},
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as Sentry from '@sentry/nuxt';
import { useRuntimeConfig } from '#imports';

Sentry.init({
environment: 'qa', // dynamic sampling bias to keep transactions
dsn: 'https://public@dsn.ingest.sentry.io/1337',
dsn: useRuntimeConfig().public.sentry.dsn,
tunnel: `http://localhost:3031/`, // proxy server
tracesSampleRate: 1.0,
});
16 changes: 12 additions & 4 deletions packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ export default defineNuxtModule<ModuleOptions>({
addPluginTemplate({
mode: 'client',
filename: 'sentry-client-config.mjs',
getContents: () =>
`import "${buildDirResolver.resolve(`/${clientConfigFile}`)}"\n` +
'import { defineNuxtPlugin } from "#imports"\n' +
'export default defineNuxtPlugin(() => {})',

// Dynamic import of config file to wrap it within a Nuxt context (here: defineNuxtPlugin)
// Makes it possible to call useRuntimeConfig() in the user-defined sentry config file
getContents: () => `
import { defineNuxtPlugin } from "#imports";

export default defineNuxtPlugin({
name: 'sentry-client-config',
async setup() {
await import("${buildDirResolver.resolve(`/${clientConfigFile}`)}")
}
});`,
});

addPlugin({ src: moduleDirResolver.resolve('./runtime/plugins/sentry.client'), mode: 'client' });
Expand Down
36 changes: 20 additions & 16 deletions packages/nuxt/src/runtime/plugins/sentry.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,28 @@ interface VueRouter {
// Tree-shakable guard to remove all code related to tracing
declare const __SENTRY_TRACING__: boolean;

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

if (sentryClient && '$router' in nuxtApp) {
sentryClient.addIntegration(
browserTracingIntegration({ router: nuxtApp.$router as VueRouter, routeLabel: 'path' }),
);
if (sentryClient && '$router' in nuxtApp) {
sentryClient.addIntegration(
browserTracingIntegration({ router: nuxtApp.$router as VueRouter, routeLabel: 'path' }),
);
}
}
}

nuxtApp.hook('app:created', vueApp => {
const sentryClient = getClient();
nuxtApp.hook('app:created', vueApp => {
const sentryClient = getClient();

if (sentryClient) {
sentryClient.addIntegration(vueIntegration({ app: vueApp }));
}
});
if (sentryClient) {
sentryClient.addIntegration(vueIntegration({ app: vueApp }));
}
});
},
});
Loading