From 3898787be1fef2200432f0e41776f3546383241c Mon Sep 17 00:00:00 2001 From: Dmitry Verkhoturov Date: Sun, 20 Oct 2024 23:51:09 +0100 Subject: [PATCH] Lazy load sentry only on errors Credit: https://www.voorhoede.nl/en/blog/load-sentry-asynchronously-only-on-error/ --- hugo/src/js/app.js | 16 ++++++++++++++-- hugo/src/js/sentry.js | 12 +++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/hugo/src/js/app.js b/hugo/src/js/app.js index 9c75e258..b83c5412 100644 --- a/hugo/src/js/app.js +++ b/hugo/src/js/app.js @@ -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'); @@ -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); +} \ No newline at end of file diff --git a/hugo/src/js/sentry.js b/hugo/src/js/sentry.js index 5e1f1832..49ddc8e9 100644 --- a/hugo/src/js/sentry.js +++ b/hugo/src/js/sentry.js @@ -1,5 +1,7 @@ -import * as Sentry from '@sentry/browser'; - -Sentry.init({ - dsn: 'https://6571368ba3af42308da7865628a950b6@sentry.io/1467904', -}); +export async function initSentry() { + const Sentry = await import('@sentry/browser'); + Sentry.init({ + dsn: 'https://6571368ba3af42308da7865628a950b6@sentry.io/1467904', + }); + return Sentry; +} \ No newline at end of file