Skip to content

Commit 4484407

Browse files
committed
Handle HTTP 304 as a successful fetch
1 parent 3f2a030 commit 4484407

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

js/http_fetcher.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,9 @@ class HTTPFetcher extends EventEmitter {
273273
signal: controller.signal
274274
});
275275

276-
if (!response.ok) {
276+
const isSuccessfulResponse = response.ok || response.status === 304;
277+
278+
if (!isSuccessfulResponse) {
277279
const { delay, errorInfo } = this.#getDelayForResponse(response);
278280
nextDelay = delay;
279281
this.emit("error", errorInfo);

tests/unit/functions/http_fetcher_spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,31 @@ describe("HTTPFetcher", () => {
5151
expect(text).toBe(responseData);
5252
});
5353

54+
it("should treat 304 responses as successful and reset error counters", async () => {
55+
server.use(
56+
http.get(TEST_URL, () => {
57+
return new HttpResponse(null, { status: 304 });
58+
})
59+
);
60+
61+
fetcher = new HTTPFetcher(TEST_URL, { reloadInterval: 60000 });
62+
fetcher.serverErrorCount = 2;
63+
fetcher.networkErrorCount = 3;
64+
65+
const responsePromise = new Promise((resolve) => {
66+
fetcher.on("response", (response) => {
67+
resolve(response);
68+
});
69+
});
70+
71+
fetcher.startPeriodicFetch();
72+
const response = await responsePromise;
73+
74+
expect(response.status).toBe(304);
75+
expect(fetcher.serverErrorCount).toBe(0);
76+
expect(fetcher.networkErrorCount).toBe(0);
77+
});
78+
5479
it("should emit error event on network failure", async () => {
5580
server.use(
5681
http.get(TEST_URL, () => {

0 commit comments

Comments
 (0)