Skip to content

Commit 944b87f

Browse files
authored
Config @sentry/nextjs (#1411)
* Config @sentry/nextjs * Upgrade @sentry/nextjs to v8 * Update CORS config * Add sentry tunnelRoute
1 parent 428aff3 commit 944b87f

File tree

10 files changed

+1221
-235
lines changed

10 files changed

+1221
-235
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,6 @@ next-env.d.ts
4545
/src/app/blog/[blogId]/data.json
4646
/src/assets/blog/main.data.json
4747
/src/assets/blog/research.data.json
48+
49+
# Sentry Config File
50+
.env.sentry-build-plugin

next.config.mjs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {withSentryConfig} from "@sentry/nextjs";
12
/** @type {import('next').NextConfig} */
23

34
// eslint-disable-next-line prettier/prettier
@@ -197,12 +198,36 @@ const nextConfig = {
197198
// })
198199

199200
const withMDX = createMDX({
200-
// Add markdown plugins here, as desired
201201
options: {
202202
remarkPlugins: [remarkGfm, remarkMath],
203203
rehypePlugins: [rehypeKatex, rehypeRaw],
204204
},
205205
})
206206

207207
// Merge MDX config with Next.js config
208-
export default withMDX(nextConfig)
208+
const configWithMDX = withMDX(nextConfig)
209+
210+
// Add Sentry configuration
211+
export default withSentryConfig(configWithMDX, {
212+
sentryUrl: process.env.SENTRY_URL,
213+
org: process.env.SENTRY_ORG,
214+
project: process.env.SENTRY_PROJECT,
215+
authToken: process.env.SENTRY_AUTH_TOKEN,
216+
217+
// Only print logs for uploading source maps in CI
218+
silent: !process.env.CI,
219+
220+
// Upload a larger set of source maps for prettier stack traces (increases build time)
221+
widenClientFileUpload: true,
222+
223+
// Hides source maps from generated client bundles
224+
hideSourceMaps: true,
225+
226+
// Automatically tree-shake Sentry logger statements to reduce bundle size
227+
disableLogger: true,
228+
229+
// Enables automatic instrumentation of Vercel Cron Monitors
230+
automaticVercelMonitors: true,
231+
232+
tunnelRoute: "/monitoring",
233+
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@next/mdx": "^15.1.5",
3030
"@next/third-parties": "^15.1.5",
3131
"@rainbow-me/rainbowkit": "^2.1.2",
32-
"@sentry/nextjs": "^7.95.0",
32+
"@sentry/nextjs": "^8",
3333
"@tailwindcss/postcss": "^4.0.6",
3434
"@tanstack/react-query": "^5.55.3",
3535
"@tanstack/react-virtual": "^3.11.2",

sentry.client.config.ts

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,56 @@
11
// This file configures the initialization of Sentry on the client.
22
// The config you add here will be used whenever a users loads a page in their browser.
33
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4-
// import * as Sentry from "@sentry/nextjs"
5-
6-
window.onerror = function (message, source, lineno, colno, error) {
7-
console.log("Global error captured:", error)
8-
}
9-
10-
window.addEventListener("unhandledrejection", function (event) {
11-
console.log("Unhandled rejection captured:", event.reason)
4+
import * as Sentry from "@sentry/nextjs"
5+
6+
Sentry.init({
7+
enabled: ["Staging", "Sepolia", "Mainnet"].includes(process.env.NEXT_PUBLIC_SCROLL_ENVIRONMENT),
8+
environment: process.env.NEXT_PUBLIC_SCROLL_ENVIRONMENT,
9+
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
10+
release: process.env.NEXT_PUBLIC_VERSION,
11+
autoSessionTracking: false,
12+
allowUrls: [/scroll\.io/, /localhost/, /vercel\.app/],
13+
14+
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
15+
tracesSampleRate: 1,
16+
17+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
18+
debug: false,
19+
20+
// Ignore specific error messages
21+
ignoreErrors: [
22+
// MetaMask and other wallet plugin common errors
23+
"MetaMask",
24+
"Non-Error promise rejection captured",
25+
"User rejected the request",
26+
"User denied transaction signature",
27+
// Extension injected errors
28+
"Extension context invalidated",
29+
"The message port closed before a response was received",
30+
"Failed to fetch",
31+
// RPC related errors
32+
"Internal JSON-RPC error",
33+
"JsonRpcEngine",
34+
// Common Web3 errors
35+
"User denied message signature",
36+
"Transaction was rejected",
37+
],
38+
39+
// Ignore errors from specific sources
40+
denyUrls: [
41+
// Common wallet extension sources
42+
/extensions\//i,
43+
/^chrome:\/\//i,
44+
/^chrome-extension:\/\//i,
45+
/^moz-extension:\/\//i,
46+
],
47+
48+
// Only capture errors we really care about
49+
beforeSend(event) {
50+
// Return null if error comes from extension to prevent sending to Sentry
51+
if (event.exception?.values?.[0]?.stacktrace?.frames?.some(frame => frame.filename?.includes("extension://"))) {
52+
return null
53+
}
54+
return event
55+
},
1256
})
13-
14-
// Sentry.init({
15-
// enabled: ["Staging", "Sepolia", "Mainnet"].includes(process.env.NEXT_PUBLIC_SCROLL_ENVIRONMENT),
16-
// environment: process.env.NEXT_PUBLIC_SCROLL_ENVIRONMENT,
17-
18-
// dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
19-
// release: process.env.NEXT_PUBLIC_VERSION,
20-
// autoSessionTracking: false,
21-
// // Adjust this value in production, or use tracesSampler for greater control
22-
// tracesSampleRate: 1,
23-
24-
// // Setting this option to true will print useful information to the console while you're setting up Sentry.
25-
// debug: false,
26-
27-
// replaysOnErrorSampleRate: 1.0,
28-
29-
// // This sets the sample rate to be 10%. You may want this to be 100% while
30-
// // in development and sample at a lower rate in production
31-
// replaysSessionSampleRate: 0.1,
32-
33-
// // You can remove this option if you're not planning to use the Sentry Session Replay feature:
34-
// integrations: [
35-
// new Sentry.Integrations.Breadcrumbs({
36-
// console: false,
37-
// dom: false,
38-
// fetch: false,
39-
// history: false,
40-
// sentry: false,
41-
// xhr: false,
42-
// }),
43-
// new Sentry.Replay({
44-
// // Additional Replay configuration goes in here, for example:
45-
// maskAllText: true,
46-
// blockAllMedia: true,
47-
// }),
48-
// ],
49-
// beforeSend(event) {
50-
// if (!event.exception) {
51-
// return null
52-
// }
53-
// return event
54-
// },
55-
// })

sentry.edge.config.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
// The config you add here will be used whenever one of the edge features is loaded.
33
// Note that this config is unrelated to the Vercel Edge Runtime and is also required when running locally.
44
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
5-
// import * as Sentry from "@sentry/nextjs"
5+
import * as Sentry from "@sentry/nextjs"
66

7-
// Sentry.init({
8-
// enabled: ["Sepolia", "Mainnet"].includes(process.env.NEXT_PUBLIC_SCROLL_ENVIRONMENT),
7+
Sentry.init({
8+
enabled: ["Sepolia", "Mainnet"].includes(process.env.NEXT_PUBLIC_SCROLL_ENVIRONMENT),
9+
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
10+
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
11+
tracesSampleRate: 1,
912

10-
// dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
11-
12-
// // Adjust this value in production, or use tracesSampler for greater control
13-
// tracesSampleRate: 1,
14-
15-
// // Setting this option to true will print useful information to the console while you're setting up Sentry.
16-
// debug: false,
17-
// })
13+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
14+
debug: false,
15+
})

sentry.server.config.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
// This file configures the initialization of Sentry on the server.
22
// The config you add here will be used whenever the server handles a request.
33
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
4-
// import * as Sentry from "@sentry/nextjs"
4+
import * as Sentry from "@sentry/nextjs"
55

6-
// Sentry.init({
7-
// enabled: ["Sepolia", "Mainnet"].includes(process.env.NEXT_PUBLIC_SCROLL_ENVIRONMENT),
6+
Sentry.init({
7+
enabled: ["Sepolia", "Mainnet"].includes(process.env.NEXT_PUBLIC_SCROLL_ENVIRONMENT),
8+
dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
9+
// Define how likely traces are sampled. Adjust this value in production, or use tracesSampler for greater control.
10+
tracesSampleRate: 1,
811

9-
// dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
10-
11-
// // Adjust this value in production, or use tracesSampler for greater control
12-
// tracesSampleRate: 1,
13-
14-
// // Setting this option to true will print useful information to the console while you're setting up Sentry.
15-
// debug: false,
16-
// })
12+
// Setting this option to true will print useful information to the console while you're setting up Sentry.
13+
debug: false,
14+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse } from "next/server"
2+
3+
export const dynamic = "force-dynamic"
4+
5+
// A faulty API route to test Sentry's error monitoring
6+
export function GET() {
7+
throw new Error("Sentry Example API Route Error")
8+
return NextResponse.json({ data: "Testing Sentry Error..." })
9+
}

src/app/sentry-example-page/page.tsx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"use client"
2+
3+
import * as Sentry from "@sentry/nextjs"
4+
import Head from "next/head"
5+
6+
export default function Page() {
7+
return (
8+
<div>
9+
<Head>
10+
<title>Sentry Onboarding</title>
11+
<meta name="description" content="Test Sentry for your Next.js app!" />
12+
</Head>
13+
14+
<main
15+
style={{
16+
minHeight: "100vh",
17+
display: "flex",
18+
flexDirection: "column",
19+
justifyContent: "center",
20+
alignItems: "center",
21+
}}
22+
>
23+
<h1 style={{ fontSize: "4rem", margin: "14px 0" }}>
24+
<svg
25+
style={{
26+
height: "1em",
27+
}}
28+
xmlns="http://www.w3.org/2000/svg"
29+
viewBox="0 0 200 44"
30+
>
31+
<path
32+
fill="currentColor"
33+
d="M124.32,28.28,109.56,9.22h-3.68V34.77h3.73V15.19l15.18,19.58h3.26V9.22h-3.73ZM87.15,23.54h13.23V20.22H87.14V12.53h14.93V9.21H83.34V34.77h18.92V31.45H87.14ZM71.59,20.3h0C66.44,19.06,65,18.08,65,15.7c0-2.14,1.89-3.59,4.71-3.59a12.06,12.06,0,0,1,7.07,2.55l2-2.83a14.1,14.1,0,0,0-9-3c-5.06,0-8.59,3-8.59,7.27,0,4.6,3,6.19,8.46,7.52C74.51,24.74,76,25.78,76,28.11s-2,3.77-5.09,3.77a12.34,12.34,0,0,1-8.3-3.26l-2.25,2.69a15.94,15.94,0,0,0,10.42,3.85c5.48,0,9-2.95,9-7.51C79.75,23.79,77.47,21.72,71.59,20.3ZM195.7,9.22l-7.69,12-7.64-12h-4.46L186,24.67V34.78h3.84V24.55L200,9.22Zm-64.63,3.46h8.37v22.1h3.84V12.68h8.37V9.22H131.08ZM169.41,24.8c3.86-1.07,6-3.77,6-7.63,0-4.91-3.59-8-9.38-8H154.67V34.76h3.8V25.58h6.45l6.48,9.2h4.44l-7-9.82Zm-10.95-2.5V12.6h7.17c3.74,0,5.88,1.77,5.88,4.84s-2.29,4.86-5.84,4.86Z M29,2.26a4.67,4.67,0,0,0-8,0L14.42,13.53A32.21,32.21,0,0,1,32.17,40.19H27.55A27.68,27.68,0,0,0,12.09,17.47L6,28a15.92,15.92,0,0,1,9.23,12.17H4.62A.76.76,0,0,1,4,39.06l2.94-5a10.74,10.74,0,0,0-3.36-1.9l-2.91,5a4.54,4.54,0,0,0,1.69,6.24A4.66,4.66,0,0,0,4.62,44H19.15a19.4,19.4,0,0,0-8-17.31l2.31-4A23.87,23.87,0,0,1,23.76,44H36.07a35.88,35.88,0,0,0-16.41-31.8l4.67-8a.77.77,0,0,1,1.05-.27c.53.29,20.29,34.77,20.66,35.17a.76.76,0,0,1-.68,1.13H40.6q.09,1.91,0,3.81h4.78A4.59,4.59,0,0,0,50,39.43a4.49,4.49,0,0,0-.62-2.28Z"
34+
></path>
35+
</svg>
36+
</h1>
37+
38+
<p>Get started by sending us a sample error:</p>
39+
<button
40+
type="button"
41+
style={{
42+
padding: "12px",
43+
cursor: "pointer",
44+
backgroundColor: "#AD6CAA",
45+
borderRadius: "4px",
46+
border: "none",
47+
color: "white",
48+
fontSize: "14px",
49+
margin: "18px",
50+
}}
51+
onClick={async () => {
52+
await Sentry.startSpan(
53+
{
54+
name: "Example Frontend Span",
55+
op: "test",
56+
},
57+
async () => {
58+
const res = await fetch("/api/sentry-example-api")
59+
if (!res.ok) {
60+
throw new Error("Sentry Example Frontend Error")
61+
}
62+
},
63+
)
64+
}}
65+
>
66+
Throw error!
67+
</button>
68+
69+
<p>
70+
Next, look for the error on the <a href="https://sentry.scroll.io/organizations/scroll/issues/?project=2">Issues Page</a>.
71+
</p>
72+
<p style={{ marginTop: "24px" }}>
73+
For more information, see{" "}
74+
<a href="https://docs.sentry.io/platforms/javascript/guides/nextjs/">https://docs.sentry.io/platforms/javascript/guides/nextjs/</a>
75+
</p>
76+
</main>
77+
</div>
78+
)
79+
}

src/instrumentation.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as Sentry from "@sentry/nextjs"
2+
3+
export async function register() {
4+
if (process.env.NEXT_RUNTIME === "nodejs") {
5+
await import("../sentry.server.config")
6+
}
7+
8+
if (process.env.NEXT_RUNTIME === "edge") {
9+
await import("../sentry.edge.config")
10+
}
11+
}
12+
13+
export const onRequestError = Sentry.captureRequestError

0 commit comments

Comments
 (0)