Unverified Commit 7a9ee4b9 authored by Kazuhito Hokamura's avatar Kazuhito Hokamura Committed by GitHub

Update Sentry example to improve display of server errors (#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">
parent b1ef76df
...@@ -23,6 +23,9 @@ const COMMIT_SHA = ...@@ -23,6 +23,9 @@ const COMMIT_SHA =
process.env.SENTRY_DSN = SENTRY_DSN process.env.SENTRY_DSN = SENTRY_DSN
module.exports = withSourceMaps({ module.exports = withSourceMaps({
serverRuntimeConfig: {
rootDir: __dirname,
},
webpack: (config, options) => { webpack: (config, options) => {
// In `pages/_app.js`, Sentry is imported from @sentry/browser. While // In `pages/_app.js`, Sentry is imported from @sentry/browser. While
// @sentry/node will run in a Node.js environment. @sentry/node will use // @sentry/node will run in a Node.js environment. @sentry/node will use
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
}, },
"dependencies": { "dependencies": {
"@sentry/browser": "^5.15.5", "@sentry/browser": "^5.15.5",
"@sentry/integrations": "5.20.1",
"@sentry/node": "^5.15.5", "@sentry/node": "^5.15.5",
"@sentry/webpack-plugin": "^1.11.1", "@sentry/webpack-plugin": "^1.11.1",
"@zeit/next-source-maps": "0.0.4-canary.1", "@zeit/next-source-maps": "0.0.4-canary.1",
......
import * as Sentry from '@sentry/node' import * as Sentry from '@sentry/node'
import { RewriteFrames } from '@sentry/integrations'
import getConfig from 'next/config'
if (process.env.NEXT_PUBLIC_SENTRY_DSN) { if (process.env.NEXT_PUBLIC_SENTRY_DSN) {
const config = getConfig()
const distDir = `${config.serverRuntimeConfig.rootDir}/.next`
Sentry.init({ Sentry.init({
enabled: process.env.NODE_ENV === 'production', 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, dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment