From 94bdfebc452f4384c02dbbd70e130441d8f4045c Mon Sep 17 00:00:00 2001 From: Jade Geels Date: Tue, 17 Dec 2024 13:41:25 +0000 Subject: [PATCH] Add browser deprecations (#8) --- README.md | 27 +++++++++++++++++++++++++++ config/rapidez/sentry-vue.php | 12 ++++++++++++ resources/js/package.js | 15 ++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 37ed608..b99fa4b 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,33 @@ This functionality can be disabled in your `.env`: SENTRY_VUE_ALLOW_TEST_ERRORS=false ``` +## Deprecating older browsers + +You may end up having a lot of errors caused by people using really old browsers that don't support some more modern widely supported functions. To combat this, you can use the `deprecations` section in the configuration file: + +```php +'deprecations' => [ + 'String.prototype.replaceAll', + 'Array.prototype.at', +] +``` + +Before initializing Sentry, this package will first check whether any of the given variables/functions are nullish (null or undefined). These are checked with `window` as the base variable. +If *any* of them end up being nullish, Sentry will not be loaded and frontend errors will not be logged. + +## Filtering errors + +You can use the standard Sentry configuration for `ignoreErrors` as described in the [sentry documentation](https://docs.sentry.io/platforms/javascript/guides/vue/configuration/filtering/#using-ignore-errors). + +This can be done in the configuration file like so: + +```php +'ignoreErrors' => [ + 'AbortError', + '_isDestroyed', +], +``` + ## License GNU General Public License v3. Please see [License File](LICENSE) for more information. diff --git a/config/rapidez/sentry-vue.php b/config/rapidez/sentry-vue.php index c67094e..89c1924 100644 --- a/config/rapidez/sentry-vue.php +++ b/config/rapidez/sentry-vue.php @@ -8,6 +8,18 @@ // Amount of errors to be logged to sentry (1.00 = 100%) 'sampleRate' => env('SENTRY_VUE_SAMPLE_RATE', 100) / 100, + + // See the Sentry documentation: https://docs.sentry.io/platforms/javascript/guides/vue/configuration/filtering/#using-ignore-errors + 'ignoreErrors' => [ + // 'AbortError', + ], + + // If any of the following variables/functions are nullish (null or undefined), do not enable Sentry. + // This helps avoid errors that are only caused by extremely old browsers. + 'deprecations' => [ + // replaceAll has been implemented on all browsers since late 2020. Uncomment to use. + // 'String.prototype.replaceAll', + ], ], // For integrations, see: https://docs.sentry.io/platforms/javascript/guides/vue/configuration/integrations/ diff --git a/resources/js/package.js b/resources/js/package.js index 32dc02f..b62cf3a 100644 --- a/resources/js/package.js +++ b/resources/js/package.js @@ -1,3 +1,16 @@ if (import.meta.env.VITE_SENTRY_DSN !== undefined && window.config.sentry.enabled) { - (() => import('./sentry.js'))() + let deprecations = window.config.sentry.configuration.deprecations || [] + let deprecated = false + deprecations.forEach((deprecation) => { + let test = deprecation.split('.').reduce((value, next) => value?.[next] ?? null, window) + if (test === null) { + deprecated = true + } + }) + + if (deprecated) { + console.error('This browser is not supported. Please upgrade to a newer version.') + } else { + (() => import('./sentry.js'))() + } }