From 0fa2c02ead0ee09a2f1a197818104cdd05b1c10c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Or=C4=99ziak?= Date: Thu, 25 Jan 2024 16:33:56 +0100 Subject: [PATCH] Fix Sentry init (#19) * Fix Sentry init * Run linters --- sentry.client.config.ts | 17 ++---------- sentry.server.config.ts | 6 ++--- .../SentryClientSideInitializer.tsx | 10 +++++++ src/app/[projectID]/layout.tsx | 2 ++ src/hooks/useSentryInit.ts | 26 +++++++++++++++++++ 5 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 src/app/[projectID]/_components/SentryClientSideInitializer.tsx create mode 100644 src/hooks/useSentryInit.ts diff --git a/sentry.client.config.ts b/sentry.client.config.ts index 2a86588..9f40375 100644 --- a/sentry.client.config.ts +++ b/sentry.client.config.ts @@ -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. diff --git a/sentry.server.config.ts b/sentry.server.config.ts index df6e854..f010116 100644 --- a/sentry.server.config.ts +++ b/sentry.server.config.ts @@ -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, diff --git a/src/app/[projectID]/_components/SentryClientSideInitializer.tsx b/src/app/[projectID]/_components/SentryClientSideInitializer.tsx new file mode 100644 index 0000000..ff88c4b --- /dev/null +++ b/src/app/[projectID]/_components/SentryClientSideInitializer.tsx @@ -0,0 +1,10 @@ +'use client'; + +import useSentryInit from '../../../hooks/useSentryInit'; + +const SentryClientSideInitializer = () => { + useSentryInit(); + return null; +}; + +export default SentryClientSideInitializer; diff --git a/src/app/[projectID]/layout.tsx b/src/app/[projectID]/layout.tsx index 36c7111..d67a7e7 100644 --- a/src/app/[projectID]/layout.tsx +++ b/src/app/[projectID]/layout.tsx @@ -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; @@ -15,6 +16,7 @@ const Layout: ProjectPageT = async ({ children, params }) => { return ( + {children} ); diff --git a/src/hooks/useSentryInit.ts b/src/hooks/useSentryInit.ts new file mode 100644 index 0000000..e11c56a --- /dev/null +++ b/src/hooks/useSentryInit.ts @@ -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;