|
| 1 | +declare const __SITE_BUILD_ID__: string; |
| 2 | + |
| 3 | +const VERSION_URL = '/site-version.json'; |
| 4 | +const CHECK_INTERVAL_MS = 5 * 60 * 1000; |
| 5 | +const INITIAL_CHECK_DELAY_MS = 15 * 1000; |
| 6 | +const UPDATE_STORAGE_KEY = 'mevera:site-update-applied'; |
| 7 | +const UPDATE_QUERY_KEY = 'siteUpdate'; |
| 8 | +const RELOAD_DELAY_MS = 750; |
| 9 | + |
| 10 | +type SiteVersion = { |
| 11 | + version?: string; |
| 12 | +}; |
| 13 | + |
| 14 | +async function fetchLatestVersion(): Promise<string | null> { |
| 15 | + const response = await fetch(`${VERSION_URL}?t=${Date.now()}`, { |
| 16 | + cache: 'no-store', |
| 17 | + headers: { |
| 18 | + 'Cache-Control': 'no-cache', |
| 19 | + Pragma: 'no-cache', |
| 20 | + }, |
| 21 | + }); |
| 22 | + |
| 23 | + if (!response.ok) { |
| 24 | + return null; |
| 25 | + } |
| 26 | + |
| 27 | + const data = (await response.json()) as SiteVersion; |
| 28 | + return typeof data.version === 'string' && data.version.length > 0 ? data.version : null; |
| 29 | +} |
| 30 | + |
| 31 | +function markReloadAttempt(version: string): boolean { |
| 32 | + const previousAttempt = sessionStorage.getItem(UPDATE_STORAGE_KEY); |
| 33 | + |
| 34 | + if (previousAttempt === version) { |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + sessionStorage.setItem(UPDATE_STORAGE_KEY, version); |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +async function refreshServiceWorkerState(): Promise<void> { |
| 43 | + if (!('serviceWorker' in navigator)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const registrations = await navigator.serviceWorker.getRegistrations(); |
| 48 | + await Promise.all(registrations.map((registration) => registration.update())); |
| 49 | +} |
| 50 | + |
| 51 | +function reloadWithCacheBuster(version: string) { |
| 52 | + const nextUrl = new URL(window.location.href); |
| 53 | + nextUrl.searchParams.set(UPDATE_QUERY_KEY, version); |
| 54 | + window.location.replace(nextUrl); |
| 55 | +} |
| 56 | + |
| 57 | +function clearRuntimeCachesInBackground() { |
| 58 | + if (!('caches' in window)) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + caches.keys() |
| 63 | + .then((cacheNames) => Promise.all(cacheNames.map((cacheName) => caches.delete(cacheName)))) |
| 64 | + .catch(() => undefined); |
| 65 | +} |
| 66 | + |
| 67 | +function applyUpdate(version: string) { |
| 68 | + if (!markReloadAttempt(version)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + clearRuntimeCachesInBackground(); |
| 73 | + window.setTimeout(() => reloadWithCacheBuster(version), RELOAD_DELAY_MS); |
| 74 | +} |
| 75 | + |
| 76 | +export function installSiteUpdateCheck() { |
| 77 | + if (!import.meta.env.PROD || typeof window === 'undefined') { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const currentVersion = __SITE_BUILD_ID__; |
| 82 | + let isChecking = false; |
| 83 | + |
| 84 | + const checkForUpdate = async () => { |
| 85 | + if (isChecking || document.visibilityState === 'hidden') { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + isChecking = true; |
| 90 | + |
| 91 | + try { |
| 92 | + const latestVersion = await fetchLatestVersion(); |
| 93 | + |
| 94 | + if (latestVersion && latestVersion !== currentVersion) { |
| 95 | + applyUpdate(latestVersion); |
| 96 | + } |
| 97 | + } catch { |
| 98 | + // A failed update check should never break the site. |
| 99 | + } finally { |
| 100 | + isChecking = false; |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + window.setTimeout(checkForUpdate, INITIAL_CHECK_DELAY_MS); |
| 105 | + window.setInterval(checkForUpdate, CHECK_INTERVAL_MS); |
| 106 | + window.addEventListener('focus', checkForUpdate); |
| 107 | + document.addEventListener('visibilitychange', checkForUpdate); |
| 108 | + |
| 109 | + if ('serviceWorker' in navigator) { |
| 110 | + refreshServiceWorkerState().catch(() => undefined); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +export function scheduleSiteUpdateCheck() { |
| 115 | + if (typeof window === 'undefined') { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const install = () => installSiteUpdateCheck(); |
| 120 | + |
| 121 | + if ('requestIdleCallback' in window) { |
| 122 | + window.requestIdleCallback(install, { timeout: 5000 }); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + setTimeout(install, 0); |
| 127 | +} |
0 commit comments