Skip to content

Commit f00dfa4

Browse files
authored
feat(Sentry): Add sentry for error monitoring and browser tracing (#53)
* feat(Sentry): Add sentry for error monitoring and browser tracing * chore(deps): Bump Next.js Version
1 parent 6d52ed6 commit f00dfa4

File tree

8 files changed

+519
-1288
lines changed

8 files changed

+519
-1288
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ public/sitemap.xml
3434
public/rss.xml
3535

3636
.vercel
37+
38+
# Sentry
39+
.sentryclirc

next.config.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
const withBundleAnalyzer = require('@next/bundle-analyzer')({
22
enabled: process.env.ANALYZE === 'true',
33
});
4-
module.exports = withBundleAnalyzer({});
4+
const { withSentryConfig } = require('@sentry/nextjs');
5+
6+
const moduleExports = {
7+
reactStrictMode: true,
8+
};
9+
10+
const sentryConfig = {
11+
silent: true,
12+
};
13+
14+
module.exports = withSentryConfig(
15+
withBundleAnalyzer(moduleExports),
16+
sentryConfig
17+
);

package-lock.json

Lines changed: 375 additions & 1286 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"analyze:browser": "cross-env BUNDLE_ANALYZE=browser next build"
1616
},
1717
"dependencies": {
18+
"@sentry/nextjs": "^6.17.9",
1819
"@supabase/supabase-js": "^1.29.1",
1920
"@tailwindcss/forms": "^0.4.0",
2021
"@tailwindcss/typography": "^0.5.0",
@@ -25,7 +26,7 @@
2526
"globby": "^12.0.2",
2627
"gray-matter": "^4.0.3",
2728
"mdx-prism": "^0.3.4",
28-
"next": "^12.0.7",
29+
"next": "^12.0.10",
2930
"next-mdx-remote": "^3.0.8",
3031
"next-seo": "^4.28.1",
3132
"next-themes": "0.0.15",

pages/_error.tsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import NextErrorComponent, { ErrorProps } from 'next/error';
2+
import { NextPageContext } from 'next';
3+
4+
import * as Sentry from '@sentry/nextjs';
5+
6+
interface PageProps {
7+
hasGetInitialPropsRun?: boolean;
8+
err?: NextPageContext['err'];
9+
}
10+
11+
const MyError = ({
12+
statusCode,
13+
hasGetInitialPropsRun,
14+
err,
15+
}: ErrorProps & PageProps) => {
16+
if (!hasGetInitialPropsRun && err) {
17+
// getInitialProps is not called in case of
18+
// https://github.com/vercel/next.js/issues/8592. As a workaround, we pass
19+
// err via _app.js so it can be captured
20+
Sentry.captureException(err);
21+
// Flushing is not required in this case as it only happens on the client
22+
}
23+
24+
return <NextErrorComponent statusCode={statusCode} />;
25+
};
26+
27+
MyError.getInitialProps = async (context: NextPageContext) => {
28+
const errorInitialProps = await NextErrorComponent.getInitialProps(context);
29+
30+
const { res, err, asPath } = context;
31+
32+
// Workaround for https://github.com/vercel/next.js/issues/8592, mark when
33+
// getInitialProps has run
34+
const errorProps: ErrorProps & PageProps = {
35+
...errorInitialProps,
36+
hasGetInitialPropsRun: true,
37+
};
38+
39+
// Returning early because we don't want to log 404 errors to Sentry.
40+
if (res?.statusCode === 404) {
41+
return errorProps;
42+
}
43+
44+
// Running on the server, the response object (`res`) is available.
45+
//
46+
// Next.js will pass an err on the server if a page's data fetching methods
47+
// threw or returned a Promise that rejected
48+
//
49+
// Running on the client (browser), Next.js will provide an err if:
50+
//
51+
// - a page's `getInitialProps` threw or returned a Promise that rejected
52+
// - an exception was thrown somewhere in the React lifecycle (render,
53+
// componentDidMount, etc) that was caught by Next.js's React Error
54+
// Boundary. Read more about what types of exceptions are caught by Error
55+
// Boundaries: https://reactjs.org/docs/error-boundaries.html
56+
57+
if (err) {
58+
Sentry.captureException(err);
59+
60+
// Flushing before returning is necessary if deploying to Vercel, see
61+
// https://vercel.com/docs/platform/limits#streaming-responses
62+
await Sentry.flush(2000);
63+
64+
return errorProps;
65+
}
66+
67+
// If this point is reached, getInitialProps was called without any
68+
// information about what the error might be. This is unexpected and may
69+
// indicate a bug introduced in Next.js, so record it in Sentry
70+
Sentry.captureException(
71+
new Error(`_error.js getInitialProps missing data at path: ${asPath}`)
72+
);
73+
await Sentry.flush(2000);
74+
75+
return errorProps;
76+
};
77+
78+
export default MyError;

sentry.client.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
enabled: process.env.NODE_ENV === 'production',
11+
dsn:
12+
SENTRY_DSN ||
13+
'https://e145f94dff0d4fcaa5ef6fdb42cbeb31@o1146015.ingest.sentry.io/6214305',
14+
// Adjust this value in production, or use tracesSampler for greater control
15+
16+
integrations: [
17+
new Sentry.BrowserTracing({
18+
// custom options
19+
}),
20+
],
21+
22+
tracesSampleRate: 0.1,
23+
});

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=codebycorey
3+
defaults.project=codebycorey
4+
cli.executable=../../.npm/_npx/84807/lib/node_modules/@sentry/wizard/node_modules/@sentry/cli/bin/sentry-cli

sentry.server.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
enabled: process.env.NODE_ENV === 'production',
11+
dsn:
12+
SENTRY_DSN ||
13+
'https://e145f94dff0d4fcaa5ef6fdb42cbeb31@o1146015.ingest.sentry.io/6214305',
14+
// Adjust this value in production, or use tracesSampler for greater control
15+
// tracesSampleRate: 0.0,
16+
// ...
17+
// Note: if you want to override the automatic release value, do not set a
18+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
19+
// that it will also get attached to your source maps
20+
});

0 commit comments

Comments
 (0)