// Optimized cache clearing for UI updates - minimal latency impact if (typeof window !== 'undefined') { const buildTime = document.querySelector('meta[name="build-time"]')?.getAttribute('content'); const lastBuildTime = localStorage.getItem('lastBuildTime'); // Only clear caches when absolutely necessary const clearCachesSelectively = () => { console.log('Selectively clearing outdated caches...'); // Only clear Service Worker caches if they exist and are outdated if ('caches' in window) { caches.keys().then(names => { // Only clear Next.js related caches, not all caches const nextCaches = names.filter(name => name.includes('next') || name.includes('static') || name.includes('webpack') ); nextCaches.forEach(name => { caches.delete(name); }); }); } // Only clear specific localStorage items localStorage.removeItem('lastBuildTime'); localStorage.removeItem('lastKnownBuildTime'); }; // Only clear caches on actual new build detection (not every page load) if (buildTime && buildTime !== lastBuildTime && lastBuildTime) { console.log('New UI build detected:', buildTime, 'vs', lastBuildTime); localStorage.setItem('lastBuildTime', buildTime); // Defer cache clearing to not block initial page load setTimeout(clearCachesSelectively, 1000); } else if (buildTime && !lastBuildTime) { // First visit - just store the build time localStorage.setItem('lastBuildTime', buildTime); } // Remove aggressive development cache clearing for production performance // Only clear on localhost during development if needed if (window.location.hostname === 'localhost' && new URLSearchParams(window.location.search).has('clearCache')) { clearCachesSelectively(); } } // Enhanced chunk load error handling for production const handleChunkError = (error, source = 'unknown') => { console.error('ChunkLoadError detected from:', source, error); // Immediately clear all Next.js caches for chunk errors const clearAndReload = () => { if ('caches' in window) { caches.keys().then(names => { // Clear all Next.js and static caches const nextCaches = names.filter(name => name.includes('next') || name.includes('static') || name.includes('webpack') || name.includes('chunk') ); if (nextCaches.length > 0) { Promise.all(nextCaches.map(name => caches.delete(name))) .then(() => { console.log('Cleared caches, reloading...'); window.location.reload(true); }) .catch(() => { // Fallback reload if cache clearing fails window.location.reload(true); }); } else { window.location.reload(true); } }).catch(() => { // Fallback reload if cache access fails window.location.reload(true); }); } else { window.location.reload(true); } }; // Clear localStorage items that might be causing issues try { localStorage.removeItem('lastBuildTime'); localStorage.removeItem('lastKnownBuildTime'); } catch (e) { console.warn('Could not clear localStorage:', e); } // Immediate reload for chunk errors clearAndReload(); }; // Enhanced error listeners for chunk load errors window.addEventListener('error', function(e) { const errorMessage = e.message || ''; const errorSource = e.filename || ''; if (errorMessage.includes('ChunkLoadError') || errorMessage.includes('Loading chunk') || errorMessage.includes('Loading CSS chunk') || errorSource.includes('_next/static/chunks/')) { handleChunkError(e, 'window.error'); } }); window.addEventListener('unhandledrejection', function(e) { const reason = e.reason ? e.reason.toString() : ''; if (reason.includes('ChunkLoadError') || reason.includes('Loading chunk') || reason.includes('Loading CSS chunk')) { handleChunkError(e.reason, 'unhandledrejection'); } }); // Additional error handling for fetch failures on chunks const originalFetch = window.fetch; window.fetch = function(...args) { return originalFetch.apply(this, args).catch(error => { const url = args[0]; if (typeof url === 'string' && (url.includes('_next/static/chunks/') || url.includes('.js'))) { console.error('Fetch failed for chunk:', url, error); handleChunkError(error, 'fetch.failure'); } throw error; }); };