Skip to content

Commit

Permalink
add is production config along with production url (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfrancisco authored Sep 17, 2024
1 parent dd47a76 commit 8e8567a
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 54 deletions.
8 changes: 4 additions & 4 deletions apps/dashboard/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { rewrites } from './src/lib/rewrites.mjs';
import { headers } from './src/lib/headers.mjs';

jiti('./src/env/client-env');
const serverEnv = jiti('./src/env/server-env');
const { serverEnv } = jiti('./src/env/server-env');

/** @type {import('next').NextConfig} */
const nextConfig = {
Expand Down Expand Up @@ -40,8 +40,8 @@ export default withSentryConfig(nextConfig, {
// For all available options, see:
// https://github.com/getsentry/sentry-webpack-plugin#options

org: 'ds-pro',
project: 'engine',
org: serverEnv.SENTRY_ORG,
project: serverEnv.SENTRY_PROJECT,

authToken: serverEnv.SENTRY_AUTH_TOKEN,

Expand All @@ -58,7 +58,7 @@ export default withSentryConfig(nextConfig, {
// This can increase your server load as well as your hosting bill.
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
// side errors will fail.
tunnelRoute: '/_proxy/sentry',
tunnelRoute: '/_proxy/m',

// Hides source maps from generated client bundles
hideSourceMaps: true,
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/sentry.client.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// The config you add here will be used whenever a users loads a page in their browser.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import { clientEnv } from '@/env/client-env';
import { config } from '@/config';
import * as Sentry from '@sentry/nextjs';

Sentry.init({
enabled: clientEnv.NEXT_PUBLIC_VERCEL_ENV === 'production',
enabled: config.isSentryEnabled,
dsn: 'https://bee796b3d7d7f0364e1dc326183331f0@o4507860870299648.ingest.de.sentry.io/4507860871872592',

// Adjust this value in production, or use tracesSampler for greater control
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/sentry.edge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// Note that this config is unrelated to the Vercel Edge Runtime and is also required when running locally.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import { serverEnv } from '@/env/server-env';
import { config } from '@/config';
import * as Sentry from '@sentry/nextjs';

Sentry.init({
enabled: serverEnv.VERCEL_ENV === 'production',
enabled: config.isSentryEnabled,
dsn: 'https://bee796b3d7d7f0364e1dc326183331f0@o4507860870299648.ingest.de.sentry.io/4507860871872592',

// Adjust this value in production, or use tracesSampler for greater control
Expand Down
4 changes: 2 additions & 2 deletions apps/dashboard/sentry.server.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// The config you add here will be used whenever the server handles a request.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import { serverEnv } from '@/env/server-env';
import { config } from '@/config';
import * as Sentry from '@sentry/nextjs';

Sentry.init({
enabled: serverEnv.VERCEL_ENV === 'production',
enabled: config.isSentryEnabled,
dsn: 'https://bee796b3d7d7f0364e1dc326183331f0@o4507860870299648.ingest.de.sentry.io/4507860871872592',

// Adjust this value in production, or use tracesSampler for greater control
Expand Down
5 changes: 1 addition & 4 deletions apps/dashboard/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { Toaster } from '@ds-project/components';
import { Favicon } from '@/components';
import dynamic from 'next/dynamic';
import { getMetadata } from '@/lib/metadata';
import { clientEnv } from '@/env/client-env';

const inter = Inter({ subsets: ['latin'] });

Expand All @@ -32,9 +31,7 @@ export default function RootLayout({
<head>
<Favicon />
</head>
<AnalyticsProvider
enabled={clientEnv.NEXT_PUBLIC_VERCEL_ENV === 'production'}
>
<AnalyticsProvider>
<body
className={cn(
'flex flex-col items-center bg-zinc-100 min-h-screen',
Expand Down
15 changes: 11 additions & 4 deletions apps/dashboard/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { clientEnv } from './env/client-env';
import { serverEnv } from './env/server-env';

const pageUrl = (() => {
switch (serverEnv.VERCEL_ENV) {
switch (clientEnv.NEXT_PUBLIC_VERCEL_ENV) {
case 'production':
return 'https://getds.pro';
return `https://${clientEnv.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL}`;
case 'preview':
return `https://${serverEnv.VERCEL_URL}`;
return `https://${clientEnv.NEXT_PUBLIC_VERCEL_URL}`;
default:
return 'http://localhost:3000';
}
})();

const isProduction = clientEnv.NEXT_PUBLIC_VERCEL_ENV === 'production';

/**
* Config should not include secret environment variables
*/
export const config = {
pageUrl,
isProduction,
areAnalyticsEnabled: isProduction,
isSentryEnabled: isProduction,
FIGMA_KEY: 'figma.key',
figmaRedirectUri: `${pageUrl}/integrations/providers/figma/callback`,
gitTokensPath: 'packages/generator/tokens',
Expand Down
5 changes: 5 additions & 0 deletions apps/dashboard/src/env/client-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ export const clientEnv = createEnv({
NEXT_PUBLIC_VERCEL_ENV: z
.enum(['development', 'preview', 'production'])
.optional(),
NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL: z.string().optional(),
NEXT_PUBLIC_VERCEL_URL: z.string().optional(),
NEXT_PUBLIC_POSTHOG_KEY: z.string().min(1),
},
runtimeEnv: {
NEXT_PUBLIC_VERCEL_ENV: process.env.NEXT_PUBLIC_VERCEL_ENV,
NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL:
process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL,
NEXT_PUBLIC_VERCEL_URL: process.env.NEXT_PUBLIC_VERCEL_URL,
NEXT_PUBLIC_POSTHOG_KEY: process.env.NEXT_PUBLIC_POSTHOG_KEY,
},
});
2 changes: 2 additions & 0 deletions apps/dashboard/src/env/server-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const serverEnv = createEnv({
POSTGRES_URL: z.string().min(1),
SUPABASE_URL: z.string().url(),
SUPABASE_ANON_KEY: z.string().min(1),
SENTRY_ORG: z.string().min(1).optional(),
SENTRY_PROJECT: z.string().min(1).optional(),
SENTRY_AUTH_TOKEN: z.string().min(1).optional(),
},
experimental__runtimeEnv: process.env,
Expand Down
38 changes: 16 additions & 22 deletions apps/dashboard/src/lib/analytics/provider.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
'use client';

import { config } from '@/config';
import { clientEnv } from '@/env/client-env';
import posthog from 'posthog-js';
import { PostHogProvider } from 'posthog-js/react';
import { useEffect } from 'react';

if (config.areAnalyticsEnabled && typeof window !== 'undefined') {
posthog.init(clientEnv.NEXT_PUBLIC_POSTHOG_KEY, {
api_host: '/_proxy/a',
ui_host: 'https://eu.posthog.com',
person_profiles: 'identified_only',
capture_pageview: false,
capture_pageleave: true,
loaded: (posthog) => {
if (clientEnv.NEXT_PUBLIC_VERCEL_ENV === 'development')
posthog.debug(true);
},
});
}

interface AnalyticsProviderProps {
children: React.ReactNode;
enabled?: boolean;
}
export function AnalyticsProvider({
children,
enabled = false,
}: AnalyticsProviderProps) {
useEffect(() => {
if (enabled && typeof window !== 'undefined') {
posthog.init(clientEnv.NEXT_PUBLIC_POSTHOG_KEY, {
api_host: '/_proxy/posthog',
ui_host: 'https://eu.posthog.com',
person_profiles: 'identified_only',
capture_pageview: false,
capture_pageleave: true,
loaded: (posthog) => {
if (clientEnv.NEXT_PUBLIC_VERCEL_ENV === 'development')
posthog.debug(true);
},
});
}
}, [enabled]);

export function AnalyticsProvider({ children }: AnalyticsProviderProps) {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
}
4 changes: 2 additions & 2 deletions apps/dashboard/src/lib/rewrites.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
const rewrites = async () => {
return [
{
source: '/_proxy/posthog/static/:path*',
source: '/_proxy/a/static/:path*',
destination: 'https://eu-assets.i.posthog.com/static/:path*',
},
{
source: '/_proxy/posthog/:path*',
source: '/_proxy/a/:path*',
destination: 'https://eu.i.posthog.com/:path*',
},
];
Expand Down
18 changes: 12 additions & 6 deletions apps/dashboard/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ import {
} from './lib/middleware/utils';
import { serverEnv } from './env/server-env';

export const config = {
/**
* Match all paths except for:
* 1. /api/ routes
* 2. /_next/ (Next.js internals)
* 3. /_proxy/m (Monitoring - Sentry proxy)
* 4. /_proxy/a (Analytics - PostHog proxy)
* 5. /*.* (static files like /favicon.ico, /sitemap.xml, /robots.txt, etc.)
*/
matcher: ['/((?!api/|_next/|_proxy/m|_proxy/a|[\\w-]+\\.\\w+).*)'],
};

export async function middleware(request: NextRequest) {
const response = NextResponse.next({ request });
const url = request.nextUrl.clone();
Expand Down Expand Up @@ -57,9 +69,3 @@ export async function middleware(request: NextRequest) {

return response;
}

export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|api/|_proxy/sentry|_proxy/posthog|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
};
13 changes: 7 additions & 6 deletions turbo.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"$schema": "https://turbo.build/schema.json",
"ui": "tui",
"globalPassThroughEnv": [
"NEXT_RUNTIME",
"globalEnv": [
"NEXT_PUBLIC_POSTHOG_KEY",
"NEXT_PUBLIC_POSTHOG_HOST",
"NEXT_PUBLIC_VERCEL_ENV",
"NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL",
"NEXT_PUBLIC_VERCEL_URL",
"GITHUB_APP_ID",
"GITHUB_APP_PRIVATE_KEY",
"GITHUB_APP_CLIENT_ID",
Expand All @@ -12,10 +16,7 @@
"POSTGRES_URL",
"SUPABASE_URL",
"SUPABASE_ANON_KEY",
"NEXT_PUBLIC_POSTHOG_KEY",
"NEXT_PUBLIC_POSTHOG_HOST",
"NEXT_PUBLIC_VERCEL_ENV",
"NEXT_PUBLIC_VERCEL_URL"
"NEXT_RUNTIME"
],
"tasks": {
"topo": {
Expand Down

0 comments on commit 8e8567a

Please sign in to comment.