Skip to content

Commit

Permalink
fix: clear fetch timeout on successful response (#3768)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezrokah committed May 17, 2020
1 parent 6042383 commit 088b1a8
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/netlify-cms-lib-util/src/unsentRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ import curry from 'lodash/curry';
import flow from 'lodash/flow';
import isString from 'lodash/isString';

let controller;
let signal;
if (typeof window !== 'undefined') {
controller = window.AbortController && new AbortController();
signal = controller && controller.signal;
}
const isAbortControllerSupported = () => {
if (typeof window !== 'undefined') {
return !!window.AbortController;
}
return false;
};

const timeout = 60;
const fetchWithTimeout = (input, init) => {
if (controller && signal && !init.signal) {
setTimeout(() => controller.abort(), timeout * 1000);
return fetch(input, { ...init, signal }).catch(e => {
if (e.name === 'AbortError') {
if (init.signal || !isAbortControllerSupported()) {
return fetch(input, init);
}
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout * 1000);
return fetch(input, { ...init, signal: controller.signal })
.then(res => {
clearTimeout(timeoutId);
return res;
})
.catch(e => {
if (e.name === 'AbortError' || e.name === 'DOMException') {
throw new Error(`Request timed out after ${timeout} seconds`);
}
throw e;
});
}
return fetch(input, init);
};

const decodeParams = paramsString =>
Expand Down

0 comments on commit 088b1a8

Please sign in to comment.