Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): prevent incorrect update notifications #335

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions ui/src/hooks/useCheckForUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import * as React from 'react'
import { toast } from 'sonner'

// Helper function to compare versions
function isNewerVersion(current: string, deployed: string): boolean {
// Remove 'v' prefix if present
const cleanCurrent = current.replace(/^v/, '')
const cleanDeployed = deployed.replace(/^v/, '')

const currentParts = cleanCurrent.split('.').map(Number)
const deployedParts = cleanDeployed.split('.').map(Number)

// Compare major.minor.patch
for (let i = 0; i < 3; i++) {
if (deployedParts[i] > currentParts[i]) return true
if (deployedParts[i] < currentParts[i]) return false
}

return false
}

export function useCheckForUpdates() {
React.useEffect(() => {
if (import.meta.env.MODE !== 'production') {
Expand All @@ -9,11 +27,31 @@ export function useCheckForUpdates() {

const checkForUpdates = async () => {
try {
const response = await fetch('/version.json')
const response = await fetch(`/version.json?_=${Date.now()}`, {
cache: 'no-store',
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
Pragma: 'no-cache',
Expires: '0',
},
})

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}

const data = await response.json()
const deployedVersion = data.version

if (deployedVersion !== __APP_VERSION__) {
// Only show toast if deployed version is newer
if (isNewerVersion(__APP_VERSION__, deployedVersion)) {
// eslint-disable-next-line no-console
console.log('New version detected:', {
current: __APP_VERSION__,
deployed: deployedVersion,
timestamp: new Date().toISOString(),
})

toast(`A new version is available! v${deployedVersion}`, {
description: 'Click the Reload button to update the app.',
action: {
Expand All @@ -29,15 +67,15 @@ export function useCheckForUpdates() {
}
}

const delay = Number(import.meta.env.VITE_UPDATE_CHECK_INTERVAL || 1000 * 60)
checkForUpdates()

const delay = Number(import.meta.env.VITE_UPDATE_CHECK_INTERVAL || 1000 * 60)
if (Number.isNaN(delay)) {
console.error('Invalid update check interval:', import.meta.env.VITE_UPDATE_CHECK_INTERVAL)
return
}

const interval = setInterval(checkForUpdates, delay)

return () => clearInterval(interval)
}, [])
}
17 changes: 12 additions & 5 deletions ui/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ const replaceVersionPlugin = () => {
outDir = config.build.outDir
},
generateBundle() {
const filePath = path.resolve(__dirname, 'public/version.json')
const content = fs.readFileSync(filePath, 'utf-8')
const updatedContent = content.replace('__APP_VERSION__', version)
const newFilePath = path.resolve(outDir, 'version.json')
fs.writeFileSync(newFilePath, updatedContent, 'utf-8')
try {
const filePath = path.resolve(__dirname, 'public/version.json')
if (!fs.existsSync(filePath)) {
throw new Error(`version.json not found at ${filePath}`)
}
const content = fs.readFileSync(filePath, 'utf-8')
const updatedContent = content.replace('__APP_VERSION__', version)
const newFilePath = path.resolve(outDir, 'version.json')
fs.writeFileSync(newFilePath, updatedContent, 'utf-8')
} catch (error) {
console.error('Failed to replace version in version.json file:', error)
}
},
}
}
Expand Down
Loading