Skip to content

Commit cf3910a

Browse files
committed
use the sentry setup wizard for next js. make sure it plays nice with existing config options
1 parent f26033b commit cf3910a

File tree

7 files changed

+142
-1
lines changed

7 files changed

+142
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ node_modules/
1010

1111
# we want the default npmrc but sometimes folks put in the gh tokens, don't commit those
1212
.npmrc
13+
14+
# Sentry
15+
.sentryclirc

next.config.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// This file sets a custom webpack configuration to use your Next.js app
2+
// with Sentry.
3+
// https://nextjs.org/docs/api-reference/next.config.js/introduction
4+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
5+
const { withSentryConfig } = require('@sentry/nextjs')
6+
17
/** @type {import('next').NextConfig} */
28
const nextConfig = {
39
output: 'standalone',
@@ -8,4 +14,42 @@ const nextConfig = {
814
},
915
}
1016

11-
module.exports = nextConfig
17+
const moduleExports = {
18+
nextConfig,
19+
sentry: {
20+
// Use `hidden-source-map` rather than `source-map` as the Webpack `devtool`
21+
// for client-side builds. (This will be the default starting in
22+
// `@sentry/nextjs` version 8.0.0.) See
23+
// https://webpack.js.org/configuration/devtool/ and
24+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#use-hidden-source-map
25+
// for more information.
26+
hideSourceMaps: true,
27+
},
28+
}
29+
30+
const sentryWebpackPluginOptions = {
31+
// Additional config options for the Sentry Webpack plugin. Keep in mind that
32+
// the following options are set automatically, and overriding them is not
33+
// recommended:
34+
// release, url, org, project, authToken, configFile, stripPrefix,
35+
// urlPrefix, include, ignore
36+
37+
silent: true, // Suppresses all logs
38+
// For all available options, see:
39+
// https://github.com/getsentry/sentry-webpack-plugin#options.
40+
}
41+
42+
43+
// module.exports = withSentryConfig(
44+
// module.exports,
45+
// nextConfig,
46+
// { silent: true },
47+
// { hideSourceMaps: true },
48+
// )
49+
50+
// Make sure adding Sentry options is the last code to run before exporting, to
51+
// ensure that your source maps include changes from all other Webpack plugins
52+
module.exports = withSentryConfig(
53+
moduleExports,
54+
sentryWebpackPluginOptions
55+
)

pages/_error.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* NOTE: This requires `@sentry/nextjs` version 7.3.0 or higher.
3+
*
4+
* NOTE: If using this with `next` version 12.2.0 or lower, uncomment the
5+
* penultimate line in `CustomErrorComponent`.
6+
*
7+
* This page is loaded by Nextjs:
8+
* - on the server, when data-fetching methods throw or reject
9+
* - on the client, when `getInitialProps` throws or rejects
10+
* - on the client, when a React lifecycle method throws or rejects, and it's
11+
* caught by the built-in Nextjs error boundary
12+
*
13+
* See:
14+
* - https://nextjs.org/docs/basic-features/data-fetching/overview
15+
* - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props
16+
* - https://reactjs.org/docs/error-boundaries.html
17+
*/
18+
19+
import * as Sentry from '@sentry/nextjs'
20+
import NextErrorComponent from 'next/error'
21+
22+
const CustomErrorComponent = props => {
23+
// If you're using a Nextjs version prior to 12.2.1, uncomment this to
24+
// compensate for https://github.com/vercel/next.js/issues/8592
25+
// Sentry.captureUnderscoreErrorException(props)
26+
27+
return <NextErrorComponent statusCode={props.statusCode} />
28+
}
29+
30+
CustomErrorComponent.getInitialProps = async contextData => {
31+
// In case this is running in a serverless function, await this in order to give Sentry
32+
// time to send the error before the lambda exits
33+
await Sentry.captureUnderscoreErrorException(contextData)
34+
35+
// This will contain the status code of the response
36+
return NextErrorComponent.getInitialProps(contextData)
37+
}
38+
39+
export default CustomErrorComponent

sentry.client.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file configures the initialization of Sentry on the browser.
2+
// The config you add here will be used whenever a page is visited.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from '@sentry/nextjs'
6+
7+
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN
8+
9+
Sentry.init({
10+
dsn: SENTRY_DSN || 'https://[email protected]/4504810271408128',
11+
// Adjust this value in production, or use tracesSampler for greater control
12+
tracesSampleRate: 1.0,
13+
// ...
14+
// Note: if you want to override the automatic release value, do not set a
15+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
16+
// that it will also get attached to your source maps
17+
})

sentry.edge.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file configures the initialization of Sentry on the server.
2+
// The config you add here will be used whenever middleware or an Edge route handles a request.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from '@sentry/nextjs'
6+
7+
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN
8+
9+
Sentry.init({
10+
dsn: SENTRY_DSN || 'https://[email protected]/4504810271408128',
11+
// Adjust this value in production, or use tracesSampler for greater control
12+
tracesSampleRate: 1.0,
13+
// ...
14+
// Note: if you want to override the automatic release value, do not set a
15+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
16+
// that it will also get attached to your source maps
17+
})

sentry.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defaults.url=https://sentry.io/
2+
defaults.org=scientist-inc
3+
defaults.project=webstore
4+
cli.executable=node_modules/@sentry/cli/bin/sentry-cli

sentry.server.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file configures the initialization of Sentry on the server.
2+
// The config you add here will be used whenever the server handles a request.
3+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4+
5+
import * as Sentry from '@sentry/nextjs'
6+
7+
const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN
8+
9+
Sentry.init({
10+
dsn: SENTRY_DSN || 'https://[email protected]/4504810271408128',
11+
// Adjust this value in production, or use tracesSampler for greater control
12+
tracesSampleRate: 1.0,
13+
// ...
14+
// Note: if you want to override the automatic release value, do not set a
15+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
16+
// that it will also get attached to your source maps
17+
})

0 commit comments

Comments
 (0)