feat: version guard — prompt stale clients to refresh after deploys
All checks were successful
Build and Push Docker Image / build (push) Successful in 7m42s
All checks were successful
Build and Push Docker Image / build (push) Successful in 7m42s
- Inject NEXT_PUBLIC_BUILD_ID at build time via next.config.ts - /api/version static route returns current build ID - VersionGuard client component checks on tab focus + every 5 min - Shows persistent toast with Refresh button (no auto-reload) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
47
src/components/shared/version-guard.tsx
Normal file
47
src/components/shared/version-guard.tsx
Normal file
@@ -0,0 +1,47 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { toast } from 'sonner'
|
||||
|
||||
const CLIENT_BUILD_ID = process.env.NEXT_PUBLIC_BUILD_ID
|
||||
|
||||
export function VersionGuard() {
|
||||
const notified = useRef(false)
|
||||
|
||||
useEffect(() => {
|
||||
async function checkVersion() {
|
||||
if (notified.current) return
|
||||
try {
|
||||
const res = await fetch('/api/version', { cache: 'no-store' })
|
||||
if (!res.ok) return
|
||||
const { buildId } = await res.json()
|
||||
if (buildId && CLIENT_BUILD_ID && buildId !== CLIENT_BUILD_ID) {
|
||||
notified.current = true
|
||||
toast('A new version is available', {
|
||||
description: 'Refresh to get the latest updates.',
|
||||
duration: Infinity,
|
||||
action: {
|
||||
label: 'Refresh',
|
||||
onClick: () => window.location.reload(),
|
||||
},
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
// Network error — ignore
|
||||
}
|
||||
}
|
||||
|
||||
// Check on tab focus (covers users returning to stale tabs)
|
||||
window.addEventListener('focus', checkVersion)
|
||||
|
||||
// Also check every 5 minutes for long-lived tabs
|
||||
const interval = setInterval(checkVersion, 5 * 60 * 1000)
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('focus', checkVersion)
|
||||
clearInterval(interval)
|
||||
}
|
||||
}, [])
|
||||
|
||||
return null
|
||||
}
|
||||
Reference in New Issue
Block a user