Skip to content

Commit

Permalink
Lazy load sentry only on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
paskal committed Nov 2, 2024
1 parent d610927 commit 01adfbd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 14 additions & 2 deletions hugo/src/js/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
// TODO: fix babel-loader and use import
require('./polyfills');

let sentryInitialized = false;

async function loadSentry() {
if (!sentryInitialized) {
sentryInitialized = true;
const {initSentry} = await import('./sentry');
await initSentry();
}
}

if (process.env.NODE_ENV !== 'production') {
// Include here for dev, but inline for prod
require('./inline');
Expand All @@ -13,6 +23,8 @@ if (process.env.NODE_ENV === 'production' || process.env.MIX_TURBO) {
require('./quicklink');
}

// Lazy load Sentry only on error
if (process.env.NODE_ENV === 'production' || process.env.ENABLE_SENTRY) {
require('./sentry');
}
window.addEventListener('error', loadSentry);
window.addEventListener('unhandledrejection', loadSentry);
}
12 changes: 7 additions & 5 deletions hugo/src/js/sentry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as Sentry from '@sentry/browser';

Sentry.init({
dsn: 'https://[email protected]/1467904',
});
export async function initSentry() {
const Sentry = await import('@sentry/browser');
Sentry.init({
dsn: 'https://[email protected]/1467904',
});
return Sentry;
}

0 comments on commit 01adfbd

Please sign in to comment.