@nuxtjs/sentry

Sentry module for Nuxt.js
Features
The module enables error logging through Sentry.
- Please note that version 2.2.0 of this package removed the older
public_key and private_key options, since the updated Sentry packages don't support these anymore.
- Please note that version 2.0.0 of this package introduces a breaking change. See #30 for more information.
Setup
Nuxt.js v2.4.0+ is required, earlier versions are not supported
- Add
@nuxtjs/sentry dependency using yarn or npm to your project
- Add
@nuxtjs/sentry to modules section of nuxt.config.js
{
modules: [
'@nuxtjs/sentry',
],
sentry: {
dsn: '',
config: {},
}
}
Configure
See Options for a list of available options
Usage
Enter your DSN in the Nuxt.js config file. Additional config settings can be found here.
Usage in Vue components
In a Vue component, Sentry is available as this.$sentry, so we can call functions like
this.$sentry.captureException(new Error('example'))
where this is a Vue instance.
Usage in asyncData
While using Nuxt's asyncData method, there's $sentry object in the context:
async asyncData ({ params, $sentry }) {
try {
let { data } = await axios.get(`https://my-api/posts/${params.id}`)
return { title: data.title }
} catch (error) {
$sentry.captureException(error)
}
}
Usage in other lifecycle areas
For the other special Nuxt lifecycle areas like plugins, middleware, and nuxtServerInit, the $sentry object is also accessible through the context object like so:
async nuxtServerInit({ commit }, { $sentry }) {
try {
let { data } = await axios.get(`https://my-api/timestamp`)
commit('setTimeStamp', data)
} catch (error) {
$sentry.captureException(error)
}
}
Lazy Loading (on the client)
:warning: Please be aware that lazy loading could prevent some errors from being reported
Set lazy: true in your module options to load Sentry lazily on the client. This will prevent Sentry from being included in your main bundle but could result in some errors not being reported.
You can also pass a lazy config object in your module options (see options for more information).
Injected properties
$sentry (mocked)
Normally $sentry would always refer to the @sentry/browser API. But if we lazy load Sentry this API wont be available until Sentry has loaded. If you don't want to worry about whether Sentry is loaded or not, a mocked Sentry API is injected into the Nuxt.js context that will execute all Sentry API calls once Sentry is loaded
See: injectMock and mockApiMethods options below
$sentryReady
This method returns a Promise which will be resolved once Sentry has been loaded. You could use this instead of mocking $sentry.
Example usage:
this.$sentryReady().then((sentry) => sentry.captureMessage('Erreur!'))
(await this.$sentryReady()).captureMessage('Erreur!')
$sentryLoad
Only injected when injectLoadHook: true.
The callback you need to call to indicate that you are ready to load Sentry.
Example usage:
...
mounted() {
this.errorListener = () => {
this.$sentryLoad()
window.removeEventListener('error', errorListener)
}
window.addEventListener('error', errorListener)
},
destroyed() {
window.removeEventListener('error', this.errorListener)
}
Options
Options can be passed using either environment variables or sentry section in nuxt.config.js.
Normally, setting required DSN information would be enough.
dsn
- Type:
String
- Default:
process.env.SENTRY_DSN || ''
- If no
dsn is provided, Sentry will be initialised, but errors will not be logged. See #47 for more information about this.
lazy
- Type:
Boolean or Object
- Default:
false
- Load Sentry lazily so it's not included in your main bundle
- If
true then the default options will be used:
{
injectMock: true,
injectLoadHook: false,
mockApiMethods: true,
chunkName: 'sentry',
webpackPrefetch: false,
webpackPreload: false
}
disabled
- Type:
Boolean
- Default:
process.env.SENTRY_DISABLED || false
- Sentry will not be initialised if set to
true.
disableClientSide
- Type:
Boolean
- Default:
process.env.SENTRY_DISABLE_CLIENT_SIDE || false
disableServerSide
- Type:
Boolean
- Default:
process.env.SENTRY_DISABLE_SERVER_SIDE || false
initialize
- Type:
Boolean
- Default:
process.env.SENTRY_INITIALIZE || true
- Can be used to add the
$sentry object without initializing it, which will result in not reporting errors to Sentry when they happen but not crashing on calling the Sentry APIs.
logMockCalls
- Type:
Boolean
- Default:
true
- Whether to log calls to the mocked
$sentry object in the console
- Only applies when mocked instance is used (when
disabled, disableClientSide or disableServerSide is true)
publishRelease
sourceMapStyle
- Type:
String
- Default:
source-map
- Only has effect when
publishRelease = true
- The type of source maps generated when publishing release to Sentry. See https://webpack.js.org/configuration/devtool for a list of available options
- Note: Consider using
hidden-source-map instead. For most people, that should be a better option but due to it being a breaking change, it won't be set as the default until next major release
attachCommits
- Type:
Boolean
- Default:
process.env.SENTRY_AUTO_ATTACH_COMMITS || false
- Only has effect when
publishRelease = true
repo
- Type:
String
- Default:
process.env.SENTRY_RELEASE_REPO || ''
- Only has effect when
publishRelease = true && attachCommits = true
disableServerRelease
disableClientRelease
clientIntegrations
- Type:
Dictionary
{
Dedupe: {},
ExtraErrorData: {},
ReportingObserver: {},
RewriteFrames: {},
Vue: {attachProps: true, logErrors: this.options.dev}
}
serverIntegrations
config
- Type:
Object
- Default:
{ environment: this.options.dev ? 'development' : 'production' }
- Sentry options common to the server and client that are passed to
Sentry.init(options). See Sentry documentation at https://docs.sentry.io/error-reporting/configuration/?platform=browsernpm
- Note that
config.dsn is automatically set based on the root dsn option
- The value for
config.release is automatically inferred from the local repo unless specified manually
serverConfig
- Type:
Object
- Default:
{ }
- Specified key will override common Sentry options for server sentry plugin
clientConfig
- Type:
Object
- Default:
{ }
- Specified keys will override common Sentry options for client sentry plugin
webpackConfig
Submitting releases to Sentry
Support for the sentry-webpack-plugin was introduced #a6cd8d3. This can be used to send releases to Sentry. Use the publishRelease option to enable this feature.
Note that releases are only submitted to Sentry when (options.publishRelease && !isDev) is true.
License
MIT License