Skip to content

Commit 8d91a1c

Browse files
authored
Merge pull request #253 from blackjid/fix_load_stale_cache_skip_api
fix fetch from api after loading stale cache when skip api set
2 parents 2f4bbbb + e556c74 commit 8d91a1c

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

flagsmith-core.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

lib/flagsmith-es/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith-es",
3-
"version": "4.1.3",
3+
"version": "4.1.4",
44
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
55
"main": "./index.js",
66
"type": "module",

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "4.1.3",
3+
"version": "4.1.4",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

lib/react-native-flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "4.1.3",
3+
"version": "4.1.4",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

test/cache.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,24 @@ describe('Cache', () => {
201201
...defaultStateAlt,
202202
});
203203
});
204+
test('should get flags from API when stale cache is loaded and skipAPI is set', async () => {
205+
const onChange = jest.fn();
206+
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({
207+
cacheFlags: true,
208+
onChange,
209+
cacheOptions: { ttl: 1, skipAPI: true, loadStale: true },
210+
});
211+
await AsyncStorage.setItem('BULLET_TRAIN_DB', JSON.stringify({
212+
...defaultStateAlt,
213+
ts: new Date().valueOf() - 100,
214+
}));
215+
await flagsmith.init(initConfig);
216+
expect(onChange).toHaveBeenCalledTimes(1);
217+
expect(mockFetch).toHaveBeenCalledTimes(1);
218+
expect(getStateToCheck(flagsmith.getState())).toEqual({
219+
...defaultStateAlt,
220+
});
221+
});
204222
test('should validate flags are unchanged when fetched', async () => {
205223
const onChange = jest.fn();
206224
const { flagsmith, initConfig, AsyncStorage, mockFetch } = getFlagsmith({

0 commit comments

Comments
 (0)