diff --git a/.gitignore b/.gitignore index 1c458028..3a74e604 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,9 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts + +# Sentry +.sentryclirc + +# Sentry +next.config.original.js diff --git a/next.config.js b/next.config.js index 41c6c7dc..8698d875 100644 --- a/next.config.js +++ b/next.config.js @@ -1,3 +1,9 @@ +// This file sets a custom webpack configuration to use your Next.js app +// with Sentry. +// https://nextjs.org/docs/api-reference/next.config.js/introduction +// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/ +const { withSentryConfig } = require('@sentry/nextjs'); + /** @type {import('next').NextConfig} */ module.exports = { reactStrictMode: true, @@ -22,3 +28,9 @@ module.exports = { }; }, }; + +module.exports = withSentryConfig( + module.exports, + { silent: true }, + { hideSourcemaps: true }, +); diff --git a/package.json b/package.json index 6479e3c8..afc6c414 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "@radix-ui/react-switch": "^1.0.1", "@radix-ui/react-toast": "^1.1.2", "@radix-ui/react-tooltip": "^1.0.4", + "@sentry/nextjs": "^7.40.0", "@supabase/auth-helpers-nextjs": "^0.5.5", "@supabase/auth-ui-react": "^0.3.3", "@supabase/auth-ui-shared": "^0.1.2", diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index 42325a45..42ef407e 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -137,6 +137,7 @@ export const Home: NextPage = () => { function handleShowTimestamp(checked: boolean) { console.log("================", checked); setShouldShowTimestamp(checked); + // throw new Error("Sentry Frontend Error"); } return ( diff --git a/pages/_error.tsx b/pages/_error.tsx new file mode 100644 index 00000000..93d0a497 --- /dev/null +++ b/pages/_error.tsx @@ -0,0 +1,40 @@ +// @ts-nocheck +/** + * NOTE: This requires `@sentry/nextjs` version 7.3.0 or higher. + * + * NOTE: If using this with `next` version 12.2.0 or lower, uncomment the + * penultimate line in `CustomErrorComponent`. + * + * This page is loaded by Nextjs: + * - on the server, when data-fetching methods throw or reject + * - on the client, when `getInitialProps` throws or rejects + * - on the client, when a React lifecycle method throws or rejects, and it's + * caught by the built-in Nextjs error boundary + * + * See: + * - https://nextjs.org/docs/basic-features/data-fetching/overview + * - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props + * - https://reactjs.org/docs/error-boundaries.html + */ + +import * as Sentry from '@sentry/nextjs'; +import NextErrorComponent from 'next/error'; + +const CustomErrorComponent = props => { + // If you're using a Nextjs version prior to 12.2.1, uncomment this to + // compensate for https://github.com/vercel/next.js/issues/8592 + // Sentry.captureUnderscoreErrorException(props); + + return ; +}; + +CustomErrorComponent.getInitialProps = async contextData => { + // In case this is running in a serverless function, await this in order to give Sentry + // time to send the error before the lambda exits + await Sentry.captureUnderscoreErrorException(contextData); + + // This will contain the status code of the response + return NextErrorComponent.getInitialProps(contextData); +}; + +export default CustomErrorComponent; diff --git a/sentry.client.config.js b/sentry.client.config.js new file mode 100644 index 00000000..f7efa97b --- /dev/null +++ b/sentry.client.config.js @@ -0,0 +1,17 @@ +// This file configures the initialization of Sentry on the browser. +// The config you add here will be used whenever a page is visited. +// https://docs.sentry.io/platforms/javascript/guides/nextjs/ + +import * as Sentry from '@sentry/nextjs'; + +const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN; + +Sentry.init({ + dsn: SENTRY_DSN || 'https://787cffbb40014f4d92dd9159ddf7a562@o4504790569254912.ingest.sentry.io/4504790583214080', + // Adjust this value in production, or use tracesSampler for greater control + tracesSampleRate: 1.0, + // ... + // Note: if you want to override the automatic release value, do not set a + // `release` value here - use the environment variable `SENTRY_RELEASE`, so + // that it will also get attached to your source maps +}); diff --git a/sentry.edge.config.js b/sentry.edge.config.js new file mode 100644 index 00000000..02ba64f7 --- /dev/null +++ b/sentry.edge.config.js @@ -0,0 +1,17 @@ +// This file configures the initialization of Sentry on the server. +// The config you add here will be used whenever middleware or an Edge route handles a request. +// https://docs.sentry.io/platforms/javascript/guides/nextjs/ + +import * as Sentry from '@sentry/nextjs'; + +const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN; + +Sentry.init({ + dsn: SENTRY_DSN || 'https://787cffbb40014f4d92dd9159ddf7a562@o4504790569254912.ingest.sentry.io/4504790583214080', + // Adjust this value in production, or use tracesSampler for greater control + tracesSampleRate: 1.0, + // ... + // Note: if you want to override the automatic release value, do not set a + // `release` value here - use the environment variable `SENTRY_RELEASE`, so + // that it will also get attached to your source maps +}); diff --git a/sentry.properties b/sentry.properties new file mode 100644 index 00000000..f88ac8c7 --- /dev/null +++ b/sentry.properties @@ -0,0 +1,3 @@ +defaults.url=https://sentry.io/ +defaults.org=mofasi +defaults.project=javascript-nextjs diff --git a/sentry.server.config.js b/sentry.server.config.js new file mode 100644 index 00000000..85ab1676 --- /dev/null +++ b/sentry.server.config.js @@ -0,0 +1,17 @@ +// This file configures the initialization of Sentry on the server. +// The config you add here will be used whenever the server handles a request. +// https://docs.sentry.io/platforms/javascript/guides/nextjs/ + +import * as Sentry from '@sentry/nextjs'; + +const SENTRY_DSN = process.env.SENTRY_DSN || process.env.NEXT_PUBLIC_SENTRY_DSN; + +Sentry.init({ + dsn: SENTRY_DSN || 'https://787cffbb40014f4d92dd9159ddf7a562@o4504790569254912.ingest.sentry.io/4504790583214080', + // Adjust this value in production, or use tracesSampler for greater control + tracesSampleRate: 1.0, + // ... + // Note: if you want to override the automatic release value, do not set a + // `release` value here - use the environment variable `SENTRY_RELEASE`, so + // that it will also get attached to your source maps +});