11'use client' ;
22
3- import { useState , useEffect } from 'react' ;
3+ import { useState , useEffect , useRef } from 'react' ;
44import { RefreshCw } from 'lucide-react' ;
55
66export default function UpdateDetector ( ) {
77 const [ updateAvailable , setUpdateAvailable ] = useState ( false ) ;
8- const [ currentVersion , setCurrentVersion ] = useState < string | null > ( null ) ;
8+ const currentVersionRef = useRef < string | null > ( null ) ;
99
1010 useEffect ( ( ) => {
1111 // Only run in production
1212 if ( process . env . NODE_ENV !== 'production' ) return ;
1313
14- // Fetch the initial version when the app loads
15- fetch ( '/version.json' )
16- . then ( res => res . json ( ) )
17- . then ( data => {
18- setCurrentVersion ( data . version + '-' + data . buildTime ) ;
19- } )
20- . catch ( ( ) => { } ) ;
14+ // Function to check for updates
15+ const checkForUpdate = async ( ) => {
16+ try {
17+ const res = await fetch ( `/version.json?t=${ Date . now ( ) } ` , {
18+ cache : 'no-store'
19+ } ) ;
20+ const data = await res . json ( ) ;
21+ const remoteVersion = data . version + '-' + data . buildTime ;
22+
23+ if ( currentVersionRef . current && remoteVersion !== currentVersionRef . current ) {
24+ setUpdateAvailable ( true ) ;
25+ } else if ( ! currentVersionRef . current ) {
26+ // First load initialization
27+ currentVersionRef . current = remoteVersion ;
28+ }
29+ } catch ( e ) {
30+ // Ignore fetch errors
31+ }
32+ } ;
33+
34+ // Initial check
35+ checkForUpdate ( ) ;
2136
2237 // Check for updates every 10 minutes
23- const interval = setInterval ( ( ) => {
24- checkForUpdate ( ) ;
25- } , 10 * 60 * 1000 ) ;
38+ const interval = setInterval ( checkForUpdate , 10 * 60 * 1000 ) ;
2639
2740 // Also check when the tab becomes focused
2841 window . addEventListener ( 'focus' , checkForUpdate ) ;
@@ -33,22 +46,6 @@ export default function UpdateDetector() {
3346 } ;
3447 } , [ ] ) ;
3548
36- const checkForUpdate = async ( ) => {
37- try {
38- const res = await fetch ( `/version.json?t=${ Date . now ( ) } ` , {
39- cache : 'no-store'
40- } ) ;
41- const data = await res . json ( ) ;
42- const newVersionFingerprint = data . version + '-' + data . buildTime ;
43-
44- if ( currentVersion && newVersionFingerprint !== currentVersion ) {
45- setUpdateAvailable ( true ) ;
46- }
47- } catch ( e ) {
48- // Ignore fetch errors
49- }
50- } ;
51-
5249 if ( ! updateAvailable ) return null ;
5350
5451 return (
0 commit comments