From 0eaed69c217b0281e2b3c18f3d104df8f5f914d1 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 10 Sep 2025 06:17:47 +0000 Subject: [PATCH] Refactor: Suppress noisy console logs in local development Co-authored-by: meta.alex.r --- app/dashboard/app/providers/posthog-provider.tsx | 11 +++++++++-- app/dashboard/app/signin/auth-form.tsx | 5 +++++ .../components/posthog-user-identifier.tsx | 8 ++++++-- app/dashboard/lib/api-client.ts | 13 +++++++++++-- app/dashboard/lib/api/user.ts | 9 +++++++++ 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/dashboard/app/providers/posthog-provider.tsx b/app/dashboard/app/providers/posthog-provider.tsx index cff2eec39..3ec5deddf 100644 --- a/app/dashboard/app/providers/posthog-provider.tsx +++ b/app/dashboard/app/providers/posthog-provider.tsx @@ -11,9 +11,16 @@ import { PostHogProvider as PHProvider } from 'posthog-js/react'; export function PostHogProvider({ children }: { children: React.ReactNode }) { useEffect(() => { const posthogKey = process.env.NEXT_PUBLIC_POSTHOG_KEY; + const environmentType = process.env.NEXT_PUBLIC_ENVIRONMENT_TYPE; - // Only initialize PostHog if we have a valid key - if (posthogKey) { + // Only initialize PostHog when a key is present AND not in explicit local/dev mode + // This prevents noisy 401/404 console errors in local development when PostHog + // assets or keys are not configured. + const shouldInitPosthog = Boolean( + posthogKey && environmentType !== 'development' && environmentType !== 'local', + ); + + if (shouldInitPosthog) { posthog.init(posthogKey, { api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST || 'https://us.i.posthog.com', person_profiles: 'identified_only', // or 'always' to create profiles for anonymous users as well diff --git a/app/dashboard/app/signin/auth-form.tsx b/app/dashboard/app/signin/auth-form.tsx index e28df6793..6a3d218d0 100644 --- a/app/dashboard/app/signin/auth-form.tsx +++ b/app/dashboard/app/signin/auth-form.tsx @@ -41,6 +41,11 @@ export function AuthForm() { const checkAuth = async () => { try { + // Skip probe when no session cookie to avoid expected 401 noise in local/dev + if (typeof document !== 'undefined' && !document.cookie?.includes('session_id=')) { + setIsCheckingAuth(false); + return; + } const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/opsboard/users/me`, { method: 'GET', credentials: 'include', diff --git a/app/dashboard/components/posthog-user-identifier.tsx b/app/dashboard/components/posthog-user-identifier.tsx index 1f52fe21b..b6578640f 100644 --- a/app/dashboard/components/posthog-user-identifier.tsx +++ b/app/dashboard/components/posthog-user-identifier.tsx @@ -9,12 +9,16 @@ export function PostHogUserIdentifier() { const posthog = usePostHog(); useEffect(() => { - if (posthog && user?.id) { + // Only interact with PostHog if it's actually loaded + // This prevents errors when analytics are disabled in local development + if (!posthog || !(posthog as any).__loaded) return; + + if (user?.id) { posthog.identify(user.id, { email: user.email || undefined, name: user.full_name || undefined, }); - } else if (posthog && !user) { + } else if (!user) { posthog.reset(); } }, [posthog, user]); diff --git a/app/dashboard/lib/api-client.ts b/app/dashboard/lib/api-client.ts index 0b992281f..1c46a2bca 100644 --- a/app/dashboard/lib/api-client.ts +++ b/app/dashboard/lib/api-client.ts @@ -122,7 +122,14 @@ export async function fetchAuthenticatedApi( errorBody = 'Failed to read error response body'; } } - console.error(`[API Client] API Error ${response.status} for ${endpoint}:`, errorBody); + // Avoid noisy console errors for expected 401s in local development or during + // unauthenticated flows (e.g., landing on signin). Still construct ApiError for handling. + const isAuthError = response.status === 401 || response.status === 403; + if (!isAuthError) { + console.error(`[API Client] API Error ${response.status} for ${endpoint}:`, errorBody); + } else if (process.env.NODE_ENV !== 'production') { + console.warn(`[API Client] Auth ${response.status} for ${endpoint}`); + } throw new ApiError( `API request failed with status ${response.status}`, response.status, @@ -149,7 +156,9 @@ export async function fetchAuthenticatedApi( if (error.status === 401) { // Ensure this runs only on the client side if (typeof window !== 'undefined') { - console.warn('[API Client] Received 401 Unauthorized for ${endpoint}.'); + if (process.env.NODE_ENV !== 'production') { + console.warn(`[API Client] Received 401 Unauthorized for ${endpoint}.`); + } // Prevent infinite loops if signin page itself triggers a 401 somehow if (window.location.pathname !== '/signin') { window.location.href = '/signin'; diff --git a/app/dashboard/lib/api/user.ts b/app/dashboard/lib/api/user.ts index 6b689b79a..86760d0d1 100644 --- a/app/dashboard/lib/api/user.ts +++ b/app/dashboard/lib/api/user.ts @@ -5,6 +5,15 @@ import { fetchAuthenticatedApi, ApiError } from '../api-client'; // Import the n * Fetches the data for the currently authenticated user using the backend API. */ export const fetchUserAPI = async (): Promise => { + // In local/dev without an authenticated session, avoid hitting the backend + // to reduce console noise and unnecessary network requests. + if (typeof document !== 'undefined') { + const hasSessionCookie = document.cookie?.includes('session_id='); + if (!hasSessionCookie) { + return null; + } + } + const endpoint = '/opsboard/users/me'; // Correct backend endpoint try {