Skip to content

Commit 2c628e4

Browse files
committed
feat: support sentry
1 parent dbd734f commit 2c628e4

File tree

9 files changed

+125
-4
lines changed

9 files changed

+125
-4
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
NEXT_APP_ENV=development
22
NEXT_APP_API_HOST=https://api-staging.pietrastudio.com
3+
NEXT_PUBLIC_SENTRY_DSN=https://143e08d5e8f04e0ea6771e8bd0fc9945@o4505486562754560.ingest.sentry.io/4505486566293504

.env.production

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
NEXT_APP_ENV=production
2-
NEXT_APP_API_HOST=http://127.0.0.1:5555
2+
NEXT_APP_API_HOST=http://127.0.0.1:5555
3+
NEXT_PUBLIC_SENTRY_DSN=https://143e08d5e8f04e0ea6771e8bd0fc9945@o4505486562754560.ingest.sentry.io/4505486566293504

.env.staging

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
NEXT_APP_ENV=staging
2-
NEXT_APP_API_HOST=http://127.0.0.1:4444
2+
NEXT_APP_API_HOST=http://127.0.0.1:4444
3+
NEXT_PUBLIC_SENTRY_DSN=https://143e08d5e8f04e0ea6771e8bd0fc9945@o4505486562754560.ingest.sentry.io/4505486566293504

next.config.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
// This file sets a custom webpack configuration to use your Next.js app
2+
// with Sentry.
3+
// https://nextjs.org/docs/api-reference/next.config.js/introduction
4+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/
5+
6+
const { withSentryConfig } = require('@sentry/nextjs');
17
const path = require('path');
28
const withBundleAnalyzer = require('@next/bundle-analyzer')({
39
enabled: process.env.ANALYZE === 'true',
410
});
511
const withLess = require('next-with-less');
12+
const dayjs = require('dayjs');
613
const isProd = process.env.NODE_ENV === 'production';
714

815
const nextConfig = (phase) => {
916
const env = {
1017
NEXT_APP_ENV: process.env.NEXT_APP_ENV,
1118
NEXT_APP_API_HOST: process.env.NEXT_APP_API_HOST,
19+
NEXT_PUBLIC_SENTRY_DSN: process.env.NEXT_PUBLIC_SENTRY_DSN,
1220
};
1321
/** @type {import('next').NextConfig} */
1422
const config = {
@@ -50,8 +58,23 @@ const nextConfig = (phase) => {
5058
});
5159
return config;
5260
},
61+
sentry: {
62+
// Use `hidden-source-map` rather than `source-map` as the Webpack `devtool`
63+
// for client-side builds. (This will be the default starting in
64+
// `@sentry/nextjs` version 8.0.0.) See
65+
// https://webpack.js.org/configuration/devtool/ and
66+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/#use-hidden-source-map
67+
// for more information.
68+
hideSourceMaps: true,
69+
},
5370
};
54-
return withBundleAnalyzer(withLess(config));
71+
return withBundleAnalyzer(
72+
withSentryConfig(withLess(config), {
73+
debug: !isProd,
74+
environment: process.env.NODE_ENV,
75+
release: `${process.env.NODE_ENV}@${dayjs().format('YYYY-MM-DD HH:mm')}`,
76+
}),
77+
);
5578
};
5679

5780
module.exports = nextConfig;

sentry.client.config.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
dsn: SENTRY_DSN,
11+
// Adjust this value in production, or use tracesSampler for greater control
12+
tracesSampleRate: 1.0,
13+
// ...
14+
// Note: if you want to override the automatic release value, do not set a
15+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
16+
// that it will also get attached to your source maps
17+
// in development and sample at a lower rate in production
18+
replaysSessionSampleRate: 0.1,
19+
20+
// If the entire session is not sampled, use the below sample rate to sample
21+
// sessions when an error occurs.
22+
replaysOnErrorSampleRate: 1.0,
23+
24+
integrations: [
25+
new Sentry.Replay({
26+
// Additional SDK configuration goes in here, for example:
27+
maskAllText: true,
28+
blockAllMedia: true,
29+
}),
30+
],
31+
});

sentry.edge.config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// This file configures the initialization of Sentry on the server.
2+
// The config you add here will be used whenever middleware or an Edge route 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+
dsn: SENTRY_DSN,
11+
// Adjust this value in production, or use tracesSampler for greater control
12+
tracesSampleRate: 1.0,
13+
// ...
14+
// Note: if you want to override the automatic release value, do not set a
15+
// `release` value here - use the environment variable `SENTRY_RELEASE`, so
16+
// that it will also get attached to your source maps
17+
});

sentry.server.config.ts

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

src/pages/404.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect } from 'react';
2+
import Error from 'next/error';
23

34
import { useRouter } from 'next/router';
45

@@ -7,7 +8,8 @@ const Custom404 = () => {
78
useEffect(() => {
89
router.push('/error');
910
}, [router]);
10-
return <div></div>;
11+
// Opinionated: do not record an exception in Sentry for 404
12+
return <Error statusCode={404} />;
1113
};
1214

1315
export default Custom404;

src/pages/_error.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* This page is loaded by Nextjs:
3+
* - on the server, when data-fetching methods throw or reject
4+
* - on the client, when `getInitialProps` throws or rejects
5+
* - on the client, when a React lifecycle method throws or rejects, and it's
6+
* caught by the built-in Nextjs error boundary
7+
*
8+
* See:
9+
* - https://nextjs.org/docs/basic-features/data-fetching/overview
10+
* - https://nextjs.org/docs/api-reference/data-fetching/get-initial-props
11+
* - https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary
12+
*/
13+
14+
import * as Sentry from '@sentry/nextjs';
15+
import NextErrorComponent from 'next/error';
16+
17+
const CustomErrorComponent = (props: any) => <NextErrorComponent statusCode={props.statusCode} />;
18+
19+
CustomErrorComponent.getInitialProps = async (contextData: any) => {
20+
// In case this is running in a serverless function, await this in order to give Sentry
21+
// time to send the error before the lambda exits
22+
await Sentry.captureUnderscoreErrorException(contextData);
23+
24+
// This will contain the status code of the response
25+
return NextErrorComponent.getInitialProps(contextData);
26+
};
27+
28+
export default CustomErrorComponent;

0 commit comments

Comments
 (0)