Skip to content

Commit 7a9ee4b

Browse files
authored
Update Sentry example to improve display of server errors (vercel#15495)
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">
1 parent b1ef76d commit 7a9ee4b

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

examples/with-sentry/next.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const COMMIT_SHA =
2323
process.env.SENTRY_DSN = SENTRY_DSN
2424

2525
module.exports = withSourceMaps({
26+
serverRuntimeConfig: {
27+
rootDir: __dirname,
28+
},
2629
webpack: (config, options) => {
2730
// In `pages/_app.js`, Sentry is imported from @sentry/browser. While
2831
// @sentry/node will run in a Node.js environment. @sentry/node will use

examples/with-sentry/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
},
1010
"dependencies": {
1111
"@sentry/browser": "^5.15.5",
12+
"@sentry/integrations": "5.20.1",
1213
"@sentry/node": "^5.15.5",
1314
"@sentry/webpack-plugin": "^1.11.1",
1415
"@zeit/next-source-maps": "0.0.4-canary.1",

examples/with-sentry/pages/_app.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
import * as Sentry from '@sentry/node'
2+
import { RewriteFrames } from '@sentry/integrations'
3+
import getConfig from 'next/config'
24

35
if (process.env.NEXT_PUBLIC_SENTRY_DSN) {
6+
const config = getConfig()
7+
const distDir = `${config.serverRuntimeConfig.rootDir}/.next`
48
Sentry.init({
59
enabled: process.env.NODE_ENV === 'production',
10+
integrations: [
11+
new RewriteFrames({
12+
iteratee: (frame) => {
13+
frame.filename = frame.filename.replace(distDir, 'app:///_next')
14+
return frame
15+
},
16+
}),
17+
],
618
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
719
})
820
}

0 commit comments

Comments
 (0)