Skip to content

Commit

Permalink
Update Sentry example to improve display of server errors (#15495)
Browse files Browse the repository at this point in the history
In the Sentry example, sourcemaps is not enabled in server error.

I solved this problem by using `RewriteFrames`. On the client side, the sourcemaps path is `~/_next`, but on the server side it needs to be `~/.next` (distDir), so I rewrite it using the `iteratee`.

ref. https://docs.sentry.io/platforms/node/sourcemaps/#updating-sentry-sdk-configuration-to-support-source-maps

-----

## before
<img width="983" alt="ss2" src="https://user-images.githubusercontent.com/39471/88479997-29188580-cf8e-11ea-8d16-10ccfa6fc1a4.png">

## after
<img width="989" alt="ss1" src="https://user-images.githubusercontent.com/39471/88479995-26b62b80-cf8e-11ea-8b1f-7784b32b9e6d.png">
  • Loading branch information
hokaccha authored Aug 7, 2020
1 parent b1ef76d commit 7a9ee4b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/with-sentry/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const COMMIT_SHA =
process.env.SENTRY_DSN = SENTRY_DSN

module.exports = withSourceMaps({
serverRuntimeConfig: {
rootDir: __dirname,
},
webpack: (config, options) => {
// In `pages/_app.js`, Sentry is imported from @sentry/browser. While
// @sentry/node will run in a Node.js environment. @sentry/node will use
Expand Down
1 change: 1 addition & 0 deletions examples/with-sentry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"dependencies": {
"@sentry/browser": "^5.15.5",
"@sentry/integrations": "5.20.1",
"@sentry/node": "^5.15.5",
"@sentry/webpack-plugin": "^1.11.1",
"@zeit/next-source-maps": "0.0.4-canary.1",
Expand Down
12 changes: 12 additions & 0 deletions examples/with-sentry/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import * as Sentry from '@sentry/node'
import { RewriteFrames } from '@sentry/integrations'
import getConfig from 'next/config'

if (process.env.NEXT_PUBLIC_SENTRY_DSN) {
const config = getConfig()
const distDir = `${config.serverRuntimeConfig.rootDir}/.next`
Sentry.init({
enabled: process.env.NODE_ENV === 'production',
integrations: [
new RewriteFrames({
iteratee: (frame) => {
frame.filename = frame.filename.replace(distDir, 'app:///_next')
return frame
},
}),
],
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
})
}
Expand Down

0 comments on commit 7a9ee4b

Please sign in to comment.