'use client' import { useState, useEffect, useCallback } from 'react' import { RefreshCw, Download } from 'lucide-react' const CHECK_INTERVAL = 60_000 // check every 60s export function UpdateBanner() { const [updateAvailable, setUpdateAvailable] = useState(false) const [initialVersion, setInitialVersion] = useState(null) const [updating, setUpdating] = useState(false) const checkVersion = useCallback(async () => { try { const res = await fetch('/api/version', { cache: 'no-store' }) const { version } = await res.json() if (!version) return if (initialVersion === null) { setInitialVersion(version) } else if (version !== initialVersion) { setUpdateAvailable(true) } } catch { // network error, skip } }, [initialVersion]) useEffect(() => { checkVersion() const id = setInterval(checkVersion, CHECK_INTERVAL) return () => clearInterval(id) }, [checkVersion]) // Also check on visibility change (user returns to tab) useEffect(() => { const onVisible = () => { if (document.visibilityState === 'visible') checkVersion() } document.addEventListener('visibilitychange', onVisible) return () => document.removeEventListener('visibilitychange', onVisible) }, [checkVersion]) if (!updateAvailable) return null const handleUpdate = async () => { setUpdating(true) try { // 1. Clear all service worker caches const keys = await caches.keys() await Promise.all(keys.map((k) => caches.delete(k))) // 2. If a new service worker is waiting, activate it const reg = await navigator.serviceWorker?.getRegistration() if (reg?.waiting) { // Listen for the new SW to take control, then reload navigator.serviceWorker.addEventListener('controllerchange', () => { window.location.reload() }, { once: true }) reg.waiting.postMessage({ type: 'SKIP_WAITING' }) // Fallback reload if controllerchange doesn't fire within 3s setTimeout(() => window.location.reload(), 3000) return } // 3. If there's an active SW but no waiting one, unregister and reload if (reg) { await reg.unregister() } } catch { // If anything fails, just reload } window.location.reload() } return (

Update Available

A new version of Jefflix has been deployed

) }