Skip to content

Commit 011bf55

Browse files
authored
Merge pull request #58 from gorbak25/fix-fetching
Refetch data on transient network failures
2 parents 093f72f + bec19a8 commit 011bf55

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/RemoteAssetCache.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ class RemoteAssetCache extends AssetCache {
113113
throw new Error("Missing `#queue` instance.");
114114
}
115115

116-
if(this.#queuePromise) {
117-
return this.#queuePromise;
116+
if(!this.#queuePromise) {
117+
// optionsOverride not supported on fetch here for re-use
118+
this.#queuePromise = this.#queue.add(() => this.fetch()).catch((e) => {
119+
this.#queuePromise = undefined;
120+
throw e;
121+
});
118122
}
119123

120-
// optionsOverride not supported on fetch here for re-use
121-
this.#queuePromise = this.#queue.add(() => this.fetch());
122-
123124
return this.#queuePromise;
124125
}
125126

@@ -199,7 +200,7 @@ class RemoteAssetCache extends AssetCache {
199200

200201
return body;
201202
} catch (e) {
202-
if (this.cachedObject) {
203+
if (this.cachedObject && this.getDurationMs(this.duration) > 0) {
203204
debug(`Error fetching ${this.displayUrl}. Message: ${e.message}`);
204205
debug(`Failing gracefully with an expired cache entry.`);
205206
return super.getCachedValue();

test/QueueTest.js

+16
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,19 @@ test("Raw Fetch using fetch method (check parallel fetch promise reuse)", async
216216
await ac2.destroy();
217217
} catch (e) {}
218218
});
219+
220+
test("Refetches data on transient failures", async (t) => {
221+
let firstFetch = true;
222+
let successData = "Good data";
223+
let failData = "Transient error";
224+
let ac1 = Fetch(() => {
225+
if (firstFetch) {
226+
firstFetch = false;
227+
throw new Error(failData)
228+
}
229+
return successData;
230+
}, {duration: "0s"})
231+
232+
await t.throwsAsync(async () => await ac1.queue())
233+
t.is(await ac1.queue(), successData);
234+
})

0 commit comments

Comments
 (0)