@@ -404,6 +404,7 @@ const Flagsmith = class {
404404 try {
405405 const json = JSON . parse ( res ) ;
406406 let cachePopulated = false ;
407+ let staleCachePopulated = false ;
407408 if ( json && json . api === this . api && json . environmentID === this . environmentID ) {
408409 let setState = true ;
409410 if ( this . identity && ( json . identity !== this . identity ) ) {
@@ -418,6 +419,7 @@ const Flagsmith = class {
418419 }
419420 else if ( json . ts && this . cacheOptions . loadStale ) {
420421 this . log ( "Loading stale cache, timestamp ts:" + json . ts + " ttl: " + this . cacheOptions . ttl + " time elapsed since cache: " + ( new Date ( ) . valueOf ( ) - json . ts ) + "ms" )
422+ staleCachePopulated = true ;
421423 setState = true ;
422424 }
423425 }
@@ -439,13 +441,14 @@ const Flagsmith = class {
439441 }
440442
441443 if ( cachePopulated ) { // retrieved flags from local storage
442- const shouldFetchFlags = ! preventFetch && ( ! this . cacheOptions . skipAPI || ! cachePopulated )
444+ // fetch the flags if the cache is stale, or if we're not skipping api on cache hits
445+ const shouldFetchFlags = ! preventFetch && ( ! this . cacheOptions . skipAPI || staleCachePopulated )
443446 this . _onChange ( null ,
444447 { isFromServer : false , flagsChanged, traitsChanged } ,
445448 this . _loadedState ( null , FlagSource . CACHE , shouldFetchFlags )
446449 ) ;
447450 this . oldFlags = this . flags ;
448- if ( this . cacheOptions . skipAPI && cachePopulated ) {
451+ if ( this . cacheOptions . skipAPI && cachePopulated && ! staleCachePopulated ) {
449452 this . log ( "Skipping API, using cache" )
450453 }
451454 if ( shouldFetchFlags ) {
0 commit comments