Fix white screen: add error boundaries and improve error handling in AnalyticsProvider and useWebVitals

This commit is contained in:
2026-01-10 17:07:00 +01:00
parent 2a260abe0a
commit 832b468ea7
4 changed files with 91 additions and 43 deletions

View File

@@ -206,12 +206,14 @@ export const useWebVitals = () => {
useEffect(() => {
if (typeof window === 'undefined') return;
// Store web vitals for batch sending
const webVitals: Record<string, number> = {};
const path = window.location.pathname;
const projectMatch = path.match(/\/projects\/([^\/]+)/);
const projectId = projectMatch ? projectMatch[1] : null;
const observers: PerformanceObserver[] = [];
// Wrap everything in try-catch to prevent errors from breaking the app
try {
// Store web vitals for batch sending
const webVitals: Record<string, number> = {};
const path = window.location.pathname;
const projectMatch = path.match(/\/projects\/([^\/]+)/);
const projectId = projectMatch ? projectMatch[1] : null;
const observers: PerformanceObserver[] = [];
const sendWebVitals = async () => {
if (Object.keys(webVitals).length >= 3) { // Wait for at least FCP, LCP, CLS
@@ -319,16 +321,28 @@ export const useWebVitals = () => {
window.addEventListener('load', handleLoad);
}
return () => {
// Cleanup all observers
observers.forEach(observer => {
return () => {
// Cleanup all observers
observers.forEach(observer => {
try {
observer.disconnect();
} catch {
// Silently fail
}
});
try {
observer.disconnect();
window.removeEventListener('load', handleLoad);
} catch {
// Silently fail
}
});
window.removeEventListener('load', handleLoad);
};
};
} catch (error) {
// If Web Vitals initialization fails, don't break the app
if (process.env.NODE_ENV === 'development') {
console.warn('Web Vitals initialization failed:', error);
}
// Return empty cleanup function
return () => {};
}
}, []);
};