import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import './index.css';
import { initI18n } from './services/i18nService';
import { scheduleIdleCallback } from './utils/requestIdleCallback';
import { initPipModeListener } from './utils/pipMode';
// CRITICAL FIX: Global error handler to prevent white screen on PWA startup errors
window.addEventListener('error', event => {
// OPTIMIZATION: Suppress browser cache operation errors for videos - they're handled by retry logic
if (
event.message?.includes('ERR_CACHE_OPERATION_NOT_SUPPORTED') ||
event.filename?.includes('.mp4') ||
event.filename?.includes('.webm')
) {
// Silently ignore - retry mechanism will handle it
return;
}
console.error('🚨 Global error caught:', event.error);
// Don't prevent default - let React error boundary handle it
});
window.addEventListener('unhandledrejection', event => {
// Suppress known-safe errors (thumbnail generation failures are handled by thumbnailCacheService)
const reason = event.reason;
const reasonStr = reason ? reason.message || String(reason) : '';
const thumbnailPatterns = [
'Thumbnail generation timed out',
'Thumbnail generation skipped',
'Video failed to load for thumbnail',
];
if (thumbnailPatterns.some(p => reasonStr.includes(p))) {
// Already handled by thumbnailCacheService — suppress to avoid console spam
return;
}
console.error('🚨 Unhandled promise rejection:', event.reason);
// Don't prevent default - let React error boundary handle it
});
const rootElement = document.getElementById('root');
if (!rootElement) {
throw new Error('Could not find root element to mount to');
}
const scheduleServiceWorkerRegistration = () => {
if (!import.meta.env.PROD || typeof window === 'undefined') {
return;
}
const register = () => {
scheduleIdleCallback(
() => {
void import('./src/serviceWorkerRegistration')
.then(module => {
module.register();
})
.catch(error => {
console.error('⚠️ Deferred service worker registration failed:', error);
});
},
{ timeout: 5000 }
);
};
if (document.readyState === 'complete') {
register();
return;
}
window.addEventListener('load', register, { once: true });
};
async function bootstrap() {
try {
await initI18n();
} catch (error) {
console.warn('⚠️ i18n initialization failed, falling back to English:', error);
}
// Listen for native Android PiP transitions (if any) and toggle `html.pip-mode`.
// Safe no-op on web.
initPipModeListener();
// CRITICAL FIX: Wrap app rendering in try-catch to prevent white screen
try {
const root = ReactDOM.createRoot(rootElement!);
// ⚡ PERFORMANCE: Only use StrictMode in development to prevent double-rendering in production
if (import.meta.env.DEV) {
root.render(
Please try refreshing the page or clearing your cache.
Error: ${error instanceof Error ? error.message : 'Unknown error'}