Add update-available banner via periodic health version check
Backend /health now returns a build version; frontend polls it every 2 minutes (and on tab focus) and shows a reload banner when it changes. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
56da4c72b2
commit
04f8c9f45c
4 changed files with 98 additions and 4 deletions
|
|
@ -5,6 +5,7 @@ import { initDb } from './db.js'
|
||||||
import { noticeRoutes } from './routes/notices.js'
|
import { noticeRoutes } from './routes/notices.js'
|
||||||
|
|
||||||
const app = Fastify({ logger: true, trustProxy: true })
|
const app = Fastify({ logger: true, trustProxy: true })
|
||||||
|
const startedAt = Date.now()
|
||||||
|
|
||||||
await app.register(cookie)
|
await app.register(cookie)
|
||||||
await app.register(cors, {
|
await app.register(cors, {
|
||||||
|
|
@ -12,7 +13,7 @@ await app.register(cors, {
|
||||||
credentials: true,
|
credentials: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/health', async () => ({ status: 'healthy' }))
|
app.get('/health', async () => ({ status: 'healthy', version: process.env.BUILD_VERSION || String(startedAt) }))
|
||||||
|
|
||||||
await app.register(noticeRoutes)
|
await app.register(noticeRoutes)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,16 @@
|
||||||
import { AuthGate } from './components/AuthGate'
|
import { AuthGate } from './components/AuthGate'
|
||||||
import { NoticeBoard } from './components/NoticeBoard'
|
import { NoticeBoard } from './components/NoticeBoard'
|
||||||
|
import { UpdateBanner } from './components/UpdateBanner'
|
||||||
|
import { useVersionCheck } from './hooks/useVersionCheck'
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
|
const updateAvailable = useVersionCheck('/notices/health')
|
||||||
return (
|
return (
|
||||||
<AuthGate>
|
<>
|
||||||
{user => <NoticeBoard user={user} />}
|
<AuthGate>
|
||||||
</AuthGate>
|
{user => <NoticeBoard user={user} />}
|
||||||
|
</AuthGate>
|
||||||
|
<UpdateBanner visible={updateAvailable} />
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
44
frontend/src/components/UpdateBanner.tsx
Normal file
44
frontend/src/components/UpdateBanner.tsx
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
import { RefreshCw } from 'lucide-react'
|
||||||
|
|
||||||
|
export function UpdateBanner({ visible }: { visible: boolean }) {
|
||||||
|
if (!visible) return null
|
||||||
|
return (
|
||||||
|
<div style={{
|
||||||
|
position: 'fixed',
|
||||||
|
bottom: 0,
|
||||||
|
left: 0,
|
||||||
|
right: 0,
|
||||||
|
zIndex: 9999,
|
||||||
|
background: 'var(--sidebar)',
|
||||||
|
color: 'var(--text-light)',
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
justifyContent: 'center',
|
||||||
|
gap: '12px',
|
||||||
|
padding: '10px 16px',
|
||||||
|
fontSize: '14px',
|
||||||
|
boxShadow: '0 -2px 8px rgba(0,0,0,0.3)',
|
||||||
|
}}>
|
||||||
|
<span>A new version is available.</span>
|
||||||
|
<button
|
||||||
|
onClick={() => window.location.reload()}
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
alignItems: 'center',
|
||||||
|
gap: '6px',
|
||||||
|
background: 'var(--accent)',
|
||||||
|
color: 'var(--sidebar)',
|
||||||
|
border: 'none',
|
||||||
|
borderRadius: '4px',
|
||||||
|
padding: '6px 14px',
|
||||||
|
fontWeight: 600,
|
||||||
|
cursor: 'pointer',
|
||||||
|
fontSize: '13px',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<RefreshCw size={14} strokeWidth={1.75} />
|
||||||
|
Reload
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
43
frontend/src/hooks/useVersionCheck.ts
Normal file
43
frontend/src/hooks/useVersionCheck.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
|
||||||
|
const POLL_MS = 2 * 60 * 1000
|
||||||
|
|
||||||
|
export function useVersionCheck(healthUrl: string) {
|
||||||
|
const [updateAvailable, setUpdateAvailable] = useState(false)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let seenVersion: string | null = null
|
||||||
|
|
||||||
|
async function check() {
|
||||||
|
try {
|
||||||
|
const res = await fetch(healthUrl, { cache: 'no-store' })
|
||||||
|
if (!res.ok) return
|
||||||
|
const data = await res.json()
|
||||||
|
const v: string | undefined = data.version
|
||||||
|
if (!v) return
|
||||||
|
if (seenVersion === null) {
|
||||||
|
seenVersion = v
|
||||||
|
} else if (v !== seenVersion) {
|
||||||
|
setUpdateAvailable(true)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// network error — skip silently
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
check()
|
||||||
|
const interval = setInterval(check, POLL_MS)
|
||||||
|
|
||||||
|
function onVisible() {
|
||||||
|
if (document.visibilityState === 'visible') check()
|
||||||
|
}
|
||||||
|
document.addEventListener('visibilitychange', onVisible)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
clearInterval(interval)
|
||||||
|
document.removeEventListener('visibilitychange', onVisible)
|
||||||
|
}
|
||||||
|
}, [healthUrl])
|
||||||
|
|
||||||
|
return updateAvailable
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue