Skip to content

Commit

Permalink
Fix Sentry init (#19)
Browse files Browse the repository at this point in the history
* Fix Sentry init

* Run linters
  • Loading branch information
micorix authored Jan 25, 2024
1 parent 734eef3 commit 0fa2c02
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 18 deletions.
17 changes: 2 additions & 15 deletions sentry.client.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
import * as Sentry from '@sentry/nextjs';
import { Integrations as TracingIntegrations } from '@sentry/tracing';
import { publicRuntimeConfig } from './src/runtimeConfig';

const { APP_ENVIRONMENT, APP_FRONTEND_RELEASE, PUBLIC_SENTRY_DSN } = publicRuntimeConfig;

if (publicRuntimeConfig.PUBLIC_SENTRY_DSN) {
Sentry.init({
dsn: PUBLIC_SENTRY_DSN,
release: APP_FRONTEND_RELEASE,
environment: APP_ENVIRONMENT,
integrations: [new TracingIntegrations.BrowserTracing()],
tracesSampleRate: 1.0,
});
}
// We initialize Sentry in SentryClientSideInitializer to use environment variables.
// Empty file so that Sentry wouldn't complain about missing config.
6 changes: 3 additions & 3 deletions sentry.server.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as Sentry from '@sentry/nextjs';
import { publicRuntimeConfig } from './src/runtimeConfig';
import environment from './src/environment/server';

const { APP_ENVIRONMENT, APP_FRONTEND_RELEASE, PUBLIC_SENTRY_DSN } = publicRuntimeConfig;
const { APP_ENVIRONMENT, APP_FRONTEND_RELEASE, PUBLIC_SENTRY_DSN } = environment.publicEnvironment;

if (publicRuntimeConfig.PUBLIC_SENTRY_DSN) {
if (PUBLIC_SENTRY_DSN) {
Sentry.init({
dsn: PUBLIC_SENTRY_DSN,
release: APP_FRONTEND_RELEASE,
Expand Down
10 changes: 10 additions & 0 deletions src/app/[projectID]/_components/SentryClientSideInitializer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use client';

import useSentryInit from '../../../hooks/useSentryInit';

const SentryClientSideInitializer = () => {
useSentryInit();
return null;
};

export default SentryClientSideInitializer;
2 changes: 2 additions & 0 deletions src/app/[projectID]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ProjectPageT } from '../../types';
import { defaultMetadata } from '../../utils/seo';
import Analytics from './_components/Analytics';
import { ProjectConfig } from '../../api/projectConfig/types';
import SentryClientSideInitializer from './_components/SentryClientSideInitializer';

export const metadata = defaultMetadata;

Expand All @@ -15,6 +16,7 @@ const Layout: ProjectPageT = async ({ children, params }) => {
return (
<Providers projectConfig={projectConfig} environment={environment}>
<Analytics />
<SentryClientSideInitializer />
<AppLayout className="h-full">{children}</AppLayout>
</Providers>
);
Expand Down
26 changes: 26 additions & 0 deletions src/hooks/useSentryInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use client';

import * as Sentry from '@sentry/nextjs';
import { Integrations as TracingIntegrations } from '@sentry/tracing';
import { useEnvironment } from '../environment/environmentContext';
import { useEffect } from 'react';

const useSentryInit = () => {
const {
publicEnvironment: { APP_ENVIRONMENT, APP_FRONTEND_RELEASE, PUBLIC_SENTRY_DSN },
} = useEnvironment();
useEffect(() => {
if (PUBLIC_SENTRY_DSN) {
Sentry.init({
dsn: PUBLIC_SENTRY_DSN,
release: APP_FRONTEND_RELEASE,
environment: APP_ENVIRONMENT,
integrations: [new TracingIntegrations.BrowserTracing()],
tracesSampleRate: 1.0,
});
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
};

export default useSentryInit;

0 comments on commit 0fa2c02

Please sign in to comment.