Skip to content

Commit 0fa2c02

Browse files
authored
Fix Sentry init (#19)
* Fix Sentry init * Run linters
1 parent 734eef3 commit 0fa2c02

File tree

5 files changed

+43
-18
lines changed

5 files changed

+43
-18
lines changed

sentry.client.config.ts

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,2 @@
1-
import * as Sentry from '@sentry/nextjs';
2-
import { Integrations as TracingIntegrations } from '@sentry/tracing';
3-
import { publicRuntimeConfig } from './src/runtimeConfig';
4-
5-
const { APP_ENVIRONMENT, APP_FRONTEND_RELEASE, PUBLIC_SENTRY_DSN } = publicRuntimeConfig;
6-
7-
if (publicRuntimeConfig.PUBLIC_SENTRY_DSN) {
8-
Sentry.init({
9-
dsn: PUBLIC_SENTRY_DSN,
10-
release: APP_FRONTEND_RELEASE,
11-
environment: APP_ENVIRONMENT,
12-
integrations: [new TracingIntegrations.BrowserTracing()],
13-
tracesSampleRate: 1.0,
14-
});
15-
}
1+
// We initialize Sentry in SentryClientSideInitializer to use environment variables.
2+
// Empty file so that Sentry wouldn't complain about missing config.

sentry.server.config.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as Sentry from '@sentry/nextjs';
2-
import { publicRuntimeConfig } from './src/runtimeConfig';
2+
import environment from './src/environment/server';
33

4-
const { APP_ENVIRONMENT, APP_FRONTEND_RELEASE, PUBLIC_SENTRY_DSN } = publicRuntimeConfig;
4+
const { APP_ENVIRONMENT, APP_FRONTEND_RELEASE, PUBLIC_SENTRY_DSN } = environment.publicEnvironment;
55

6-
if (publicRuntimeConfig.PUBLIC_SENTRY_DSN) {
6+
if (PUBLIC_SENTRY_DSN) {
77
Sentry.init({
88
dsn: PUBLIC_SENTRY_DSN,
99
release: APP_FRONTEND_RELEASE,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use client';
2+
3+
import useSentryInit from '../../../hooks/useSentryInit';
4+
5+
const SentryClientSideInitializer = () => {
6+
useSentryInit();
7+
return null;
8+
};
9+
10+
export default SentryClientSideInitializer;

src/app/[projectID]/layout.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ProjectPageT } from '../../types';
77
import { defaultMetadata } from '../../utils/seo';
88
import Analytics from './_components/Analytics';
99
import { ProjectConfig } from '../../api/projectConfig/types';
10+
import SentryClientSideInitializer from './_components/SentryClientSideInitializer';
1011

1112
export const metadata = defaultMetadata;
1213

@@ -15,6 +16,7 @@ const Layout: ProjectPageT = async ({ children, params }) => {
1516
return (
1617
<Providers projectConfig={projectConfig} environment={environment}>
1718
<Analytics />
19+
<SentryClientSideInitializer />
1820
<AppLayout className="h-full">{children}</AppLayout>
1921
</Providers>
2022
);

src/hooks/useSentryInit.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use client';
2+
3+
import * as Sentry from '@sentry/nextjs';
4+
import { Integrations as TracingIntegrations } from '@sentry/tracing';
5+
import { useEnvironment } from '../environment/environmentContext';
6+
import { useEffect } from 'react';
7+
8+
const useSentryInit = () => {
9+
const {
10+
publicEnvironment: { APP_ENVIRONMENT, APP_FRONTEND_RELEASE, PUBLIC_SENTRY_DSN },
11+
} = useEnvironment();
12+
useEffect(() => {
13+
if (PUBLIC_SENTRY_DSN) {
14+
Sentry.init({
15+
dsn: PUBLIC_SENTRY_DSN,
16+
release: APP_FRONTEND_RELEASE,
17+
environment: APP_ENVIRONMENT,
18+
integrations: [new TracingIntegrations.BrowserTracing()],
19+
tracesSampleRate: 1.0,
20+
});
21+
}
22+
// eslint-disable-next-line react-hooks/exhaustive-deps
23+
}, []);
24+
};
25+
26+
export default useSentryInit;

0 commit comments

Comments
 (0)