@@ -136,8 +136,12 @@ export default function App() {
136136 // Load persisted "use index" setting
137137 const savedUseIndex = await idbSearch . get < boolean > ( 'use-index' ) ;
138138 if ( typeof savedUseIndex === 'boolean' ) setUseIndex ( savedUseIndex ) ;
139- // NOTE: do NOT call initServers here automatically. We only populate the index
140- // when the user explicitly enables the toggle in the UI.
139+
140+ // If index is enabled, check for stale data and refresh in background
141+ if ( savedUseIndex ) {
142+ if ( await idbSearch . isDataStale ( ) ) idbSearch . refreshInBackground ( registryUrl ) ;
143+ }
144+
141145 const savedApiUrl = await idbSearch . get < string > ( 'mcp-registry-api-url' ) ;
142146 if ( savedApiUrl ) setRegistryUrl ( savedApiUrl ) ;
143147 const savedResultsPerPage = await idbSearch . get < string > ( 'results-per-page' ) ;
@@ -153,6 +157,13 @@ export default function App() {
153157 setSettingsLoaded ( true ) ;
154158 }
155159 } ) ( ) ;
160+
161+ // Listen for background refresh completion, re-run the search to show updated results
162+ const handleServersUpdated = ( ) => {
163+ if ( useIndex ) fetchServers ( search , null ) ;
164+ } ;
165+ window . addEventListener ( 'servers-updated' , handleServersUpdated ) ;
166+
156167 // Listen to back/forward navigation and sync `search` with the URL
157168 if ( typeof window === 'undefined' ) return ;
158169 const onPop = ( ) => {
@@ -162,7 +173,20 @@ export default function App() {
162173 } catch { }
163174 } ;
164175 window . addEventListener ( 'popstate' , onPop ) ;
165- return ( ) => window . removeEventListener ( 'popstate' , onPop ) ;
176+
177+ // Check for stale data when page becomes visible (user returns to tab)
178+ const handleVisibilityChange = async ( ) => {
179+ if ( document . visibilityState === 'visible' && useIndex ) {
180+ if ( await idbSearch . isDataStale ( ) ) idbSearch . refreshInBackground ( registryUrl ) ;
181+ }
182+ } ;
183+ document . addEventListener ( 'visibilitychange' , handleVisibilityChange ) ;
184+
185+ return ( ) => {
186+ window . removeEventListener ( 'servers-updated' , handleServersUpdated ) ;
187+ window . removeEventListener ( 'popstate' , onPop ) ;
188+ document . removeEventListener ( 'visibilitychange' , handleVisibilityChange ) ;
189+ } ;
166190 // eslint-disable-next-line react-hooks/exhaustive-deps
167191 } , [ ] ) ;
168192
0 commit comments