From ef4f22bae8fc7e17372b2902375eed2db7f6c0d3 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Donoso Date: Tue, 17 Sep 2024 15:18:59 -0300 Subject: [PATCH 1/4] fix fetch from api after loading stale cache when skip api set --- flagsmith-core.ts | 7 +++++-- test/cache.test.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/flagsmith-core.ts b/flagsmith-core.ts index 850a0a5..a07b5e5 100644 --- a/flagsmith-core.ts +++ b/flagsmith-core.ts @@ -404,6 +404,7 @@ const Flagsmith = class { try { const json = JSON.parse(res); let cachePopulated = false; + let staleCachePopulated = false; if (json && json.api === this.api && json.environmentID === this.environmentID) { let setState = true; if (this.identity && (json.identity !== this.identity)) { @@ -418,6 +419,7 @@ const Flagsmith = class { } else if (json.ts && this.cacheOptions.loadStale) { this.log("Loading stale cache, timestamp ts:" + json.ts + " ttl: " + this.cacheOptions.ttl + " time elapsed since cache: " + (new Date().valueOf()-json.ts)+"ms") + staleCachePopulated = true; setState = true; } } @@ -439,13 +441,14 @@ const Flagsmith = class { } if (cachePopulated) { // retrieved flags from local storage - const shouldFetchFlags = !preventFetch && (!this.cacheOptions.skipAPI||!cachePopulated) + // fetch the flags if the cache is stale, or if we're not skipping api on cache hits + const shouldFetchFlags = !preventFetch && (!this.cacheOptions.skipAPI || staleCachePopulated) this._onChange(null, { isFromServer: false, flagsChanged, traitsChanged }, this._loadedState(null, FlagSource.CACHE, shouldFetchFlags) ); this.oldFlags = this.flags; - if (this.cacheOptions.skipAPI && cachePopulated) { + if (this.cacheOptions.skipAPI && cachePopulated && !staleCachePopulated) { this.log("Skipping API, using cache") } if (shouldFetchFlags) { diff --git a/test/cache.test.ts b/test/cache.test.ts index b48c8ef..60156fd 100644 --- a/test/cache.test.ts +++ b/test/cache.test.ts @@ -201,6 +201,24 @@ describe('Cache', () => { ...defaultStateAlt, }); }); + test('should get flags from API when stale cache is loaded and skipAPI is set', async () => { + const onChange = jest.fn(); + const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({ + cacheFlags: true, + onChange, + cacheOptions: { ttl: 1, skipAPI: true, loadStale: true }, + }); + await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({ + ...defaultStateAlt, + ts: new Date().valueOf() - 100, + })); + await flagsmith.init(initConfig); + expect(onChange).toHaveBeenCalledTimes(1); + expect(mockFetch).toHaveBeenCalledTimes(1); + expect(getStateToCheck(flagsmith.getState())).toEqual({ + ...defaultStateAlt, + }); + }); test('should validate flags are unchanged when fetched', async () => { const onChange = jest.fn(); const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({ From 92f0cb1608c1f22aadcf9fed39a51b913ac07561 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Tue, 24 Sep 2024 09:47:06 +0100 Subject: [PATCH 2/4] Version bump --- lib/flagsmith-es/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/flagsmith-es/package.json b/lib/flagsmith-es/package.json index 5c250e1..4651b5f 100644 --- a/lib/flagsmith-es/package.json +++ b/lib/flagsmith-es/package.json @@ -1,6 +1,6 @@ { "name": "flagsmith-es", - "version": "4.1.3", + "version": "4.1.4", "description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.", "main": "./index.js", "type": "module", From 20043a2aaa097bf62987700aa9fe2ecf40335f95 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Tue, 24 Sep 2024 09:47:22 +0100 Subject: [PATCH 3/4] Version bump --- lib/react-native-flagsmith/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/react-native-flagsmith/package.json b/lib/react-native-flagsmith/package.json index 8c333ce..cccae17 100644 --- a/lib/react-native-flagsmith/package.json +++ b/lib/react-native-flagsmith/package.json @@ -1,6 +1,6 @@ { "name": "react-native-flagsmith", - "version": "4.1.3", + "version": "4.1.4", "description": "Feature flagging to support continuous development", "main": "./index.js", "repository": { From e556c747bf78e0b0dcd1042243f7f26bdfe981cc Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Tue, 24 Sep 2024 09:47:42 +0100 Subject: [PATCH 4/4] Version bump --- lib/flagsmith/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/flagsmith/package.json b/lib/flagsmith/package.json index 737cb3c..de12a3e 100644 --- a/lib/flagsmith/package.json +++ b/lib/flagsmith/package.json @@ -1,6 +1,6 @@ { "name": "flagsmith", - "version": "4.1.3", + "version": "4.1.4", "description": "Feature flagging to support continuous development", "main": "./index.js", "repository": {