diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f4a23f..fa7e2ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 2.6.1 + +- optimize "Failed to fetch" retry case [147](https://github.com/i18next/i18next-http-backend/issues/147) + ### 2.6.0 - fix "Failed to fetch" retry case [147](https://github.com/i18next/i18next-http-backend/issues/147) diff --git a/i18nextHttpBackend.js b/i18nextHttpBackend.js index 5abdf4e..d2d55ae 100644 --- a/i18nextHttpBackend.js +++ b/i18nextHttpBackend.js @@ -131,7 +131,7 @@ var Backend = function () { this.options.request(this.options, url, payload, function (err, res) { if (res && (res.status >= 500 && res.status < 600 || !res.status)) return callback('failed loading ' + url + '; status code: ' + res.status, true); if (res && res.status >= 400 && res.status < 500) return callback('failed loading ' + url + '; status code: ' + res.status, false); - if (!res && err && err.message && (err.message.indexOf('Failed to fetch') > -1 || err.message.indexOf('failed') > -1 && err.message.indexOf('fetch') > -1)) return callback('failed loading ' + url + ': ' + err.message, true); + if (!res && err && err.message && err.message.toLowerCase().indexOf('failed') > -1 && (err.message.indexOf('fetch') > -1 || err.message.toLowerCase().indexOf('network') > -1)) return callback('failed loading ' + url + ': ' + err.message, true); if (err) return callback(err, false); var ret, parseErr; try { diff --git a/i18nextHttpBackend.min.js b/i18nextHttpBackend.min.js index 8d0157e..9166d31 100644 --- a/i18nextHttpBackend.min.js +++ b/i18nextHttpBackend.min.js @@ -1 +1 @@ -!function(e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).i18nextHttpBackend=e()}(function(){return function o(r,i,a){function s(t,e){if(!i[t]){if(!r[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(f)return f(t,!0);throw(e=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",e}n=i[t]={exports:{}},r[t][0].call(n.exports,function(e){return s(r[t][1][e]||e)},n,n.exports,o,r,i,a)}return i[t].exports}for(var f="function"==typeof require&&require,e=0;e{if("object"!=i(e)||!e)return e;var n=e[Symbol.toPrimitive];if(void 0===n)return("string"===t?String:Number)(e);if("object"!=i(n=n.call(e,t||"default")))return n;throw new TypeError("@@toPrimitive must return a primitive value.")})(e,"string");return"symbol"==i(e)?e:e+""}e=function e(t){var n=1{if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=w(e)&&"function"!=typeof e)return{default:e};if((t=a(t))&&t.has(e))return t.get(e);var n,o,r={__proto__:null},i=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(n in e)"default"!==n&&{}.hasOwnProperty.call(e,n)&&((o=i?Object.getOwnPropertyDescriptor(e,n):null)&&(o.get||o.set)?Object.defineProperty(r,n,o):r[n]=e[n]);return r.default=e,t&&t.set(e,r),r})(n("./getFetch.js"));function a(e){var t,n;return"function"!=typeof WeakMap?null:(t=new WeakMap,n=new WeakMap,(a=function(e){return e?n:t})(e))}function t(t,e){var n,o=Object.keys(t);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(t),e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),o.push.apply(o,n)),o}function g(o){for(var e=1;e(e=((e,t)=>{if("object"!=w(e)||!e)return e;var n=e[Symbol.toPrimitive];if(void 0===n)return("string"===t?String:Number)(e);if("object"!=w(n=n.call(e,t||"default")))return n;throw new TypeError("@@toPrimitive must return a primitive value.")})(e,"string"),"symbol"==w(e)?e:e+""))(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n}):Object.getOwnPropertyDescriptors?Object.defineProperties(o,Object.getOwnPropertyDescriptors(r)):t(Object(r)).forEach(function(e){Object.defineProperty(o,e,Object.getOwnPropertyDescriptor(r,e))})}return o}function w(e){return(w="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}"function"==typeof fetch&&(h=void 0!==S&&S.fetch?S.fetch:"undefined"!=typeof window&&window.fetch?window.fetch:fetch),(0,m.hasXMLHttpRequest)()&&(void 0!==S&&S.XMLHttpRequest?b=S.XMLHttpRequest:"undefined"!=typeof window&&window.XMLHttpRequest&&(b=window.XMLHttpRequest)),"function"==typeof ActiveXObject&&(void 0!==S&&S.ActiveXObject?v=S.ActiveXObject:"undefined"!=typeof window&&window.ActiveXObject&&(v=window.ActiveXObject)),"function"!=typeof(h=h||!e||b||v?h:e.default||e)&&(h=void 0);var O=function(e,t){if(t&&"object"===w(t)){var n,o="";for(n in t)o+="&"+encodeURIComponent(n)+"="+encodeURIComponent(t[n]);if(!o)return e;e=e+(-1!==e.indexOf("?")?"&":"?")+o.slice(1)}return e},j=function(e,t,n,o){function r(t){if(!t.ok)return n(t.statusText||"Error",{status:t.status});t.text().then(function(e){n(null,{status:t.status,data:e})}).catch(n)}if(o){o=o(e,t);if(o instanceof Promise)return void o.then(r).catch(n)}("function"==typeof fetch?fetch:h)(e,t).then(r).catch(n)},P=!1;r.default=function(e,t,n,o){if("function"==typeof n&&(o=n,n=void 0),o=o||function(){},h&&0!==t.indexOf("file:")){var r=e,i=t,a=n,s=o,f=(r.queryStringParams&&(i=O(i,r.queryStringParams)),g({},"function"==typeof r.customHeaders?r.customHeaders():r.customHeaders)),u=("undefined"==typeof window&&void 0!==S&&void 0!==S.process&&S.process.versions&&S.process.versions.node&&(f["User-Agent"]="i18next-http-backend (node/".concat(S.process.version,"; ").concat(S.process.platform," ").concat(S.process.arch,")")),a&&(f["Content-Type"]="application/json"),"function"==typeof r.requestOptions?r.requestOptions(a):r.requestOptions),c=g({method:a?"POST":"GET",body:a?r.stringify(a):void 0,headers:f},P?{}:u),a="function"==typeof r.alternateFetch&&1<=r.alternateFetch.length?r.alternateFetch:void 0;try{j(i,c,s,a)}catch(e){if(!u||0===Object.keys(u).length||!e.message||e.message.indexOf("not implemented")<0)return s(e);try{Object.keys(u).forEach(function(e){delete c[e]}),j(i,c,s,a),P=!0}catch(e){s(e)}}}else if((0,m.hasXMLHttpRequest)()||"function"==typeof ActiveXObject){var f=e,r=t,u=n,l=o;u&&"object"===w(u)&&(u=O("",u).slice(1)),f.queryStringParams&&(r=O(r,f.queryStringParams));try{var d=b?new b:new v("MSXML2.XMLHTTP.3.0"),p=(d.open(u?"POST":"GET",r,1),f.crossDomain||d.setRequestHeader("X-Requested-With","XMLHttpRequest"),d.withCredentials=!!f.withCredentials,u&&d.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),d.overrideMimeType&&d.overrideMimeType("application/json"),f.customHeaders);if(p="function"==typeof p?p():p)for(var y in p)d.setRequestHeader(y,p[y]);d.onreadystatechange=function(){3e&&"function"==typeof e.then)(e))return e;return Promise.resolve(e)};var n=[],r=n.forEach,i=n.slice},{}],5:[function(e,t,n){},{}]},{},[2])(2)}); \ No newline at end of file +!function(e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).i18nextHttpBackend=e()}(function(){return function o(r,i,a){function s(t,e){if(!i[t]){if(!r[t]){var n="function"==typeof require&&require;if(!e&&n)return n(t,!0);if(f)return f(t,!0);throw(e=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",e}n=i[t]={exports:{}},r[t][0].call(n.exports,function(e){return s(r[t][1][e]||e)},n,n.exports,o,r,i,a)}return i[t].exports}for(var f="function"==typeof require&&require,e=0;e{if("object"!=i(e)||!e)return e;var n=e[Symbol.toPrimitive];if(void 0===n)return("string"===t?String:Number)(e);if("object"!=i(n=n.call(e,t||"default")))return n;throw new TypeError("@@toPrimitive must return a primitive value.")})(e,"string");return"symbol"==i(e)?e:e+""}e=function e(t){var n=1{if(!t&&e&&e.__esModule)return e;if(null===e||"object"!=w(e)&&"function"!=typeof e)return{default:e};if((t=a(t))&&t.has(e))return t.get(e);var n,o,r={__proto__:null},i=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(n in e)"default"!==n&&{}.hasOwnProperty.call(e,n)&&((o=i?Object.getOwnPropertyDescriptor(e,n):null)&&(o.get||o.set)?Object.defineProperty(r,n,o):r[n]=e[n]);return r.default=e,t&&t.set(e,r),r})(n("./getFetch.js"));function a(e){var t,n;return"function"!=typeof WeakMap?null:(t=new WeakMap,n=new WeakMap,(a=function(e){return e?n:t})(e))}function t(t,e){var n,o=Object.keys(t);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(t),e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),o.push.apply(o,n)),o}function g(o){for(var e=1;e(e=((e,t)=>{if("object"!=w(e)||!e)return e;var n=e[Symbol.toPrimitive];if(void 0===n)return("string"===t?String:Number)(e);if("object"!=w(n=n.call(e,t||"default")))return n;throw new TypeError("@@toPrimitive must return a primitive value.")})(e,"string"),"symbol"==w(e)?e:e+""))(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n}):Object.getOwnPropertyDescriptors?Object.defineProperties(o,Object.getOwnPropertyDescriptors(r)):t(Object(r)).forEach(function(e){Object.defineProperty(o,e,Object.getOwnPropertyDescriptor(r,e))})}return o}function w(e){return(w="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}"function"==typeof fetch&&(h=void 0!==S&&S.fetch?S.fetch:"undefined"!=typeof window&&window.fetch?window.fetch:fetch),(0,m.hasXMLHttpRequest)()&&(void 0!==S&&S.XMLHttpRequest?b=S.XMLHttpRequest:"undefined"!=typeof window&&window.XMLHttpRequest&&(b=window.XMLHttpRequest)),"function"==typeof ActiveXObject&&(void 0!==S&&S.ActiveXObject?v=S.ActiveXObject:"undefined"!=typeof window&&window.ActiveXObject&&(v=window.ActiveXObject)),"function"!=typeof(h=h||!e||b||v?h:e.default||e)&&(h=void 0);var O=function(e,t){if(t&&"object"===w(t)){var n,o="";for(n in t)o+="&"+encodeURIComponent(n)+"="+encodeURIComponent(t[n]);if(!o)return e;e=e+(-1!==e.indexOf("?")?"&":"?")+o.slice(1)}return e},j=function(e,t,n,o){function r(t){if(!t.ok)return n(t.statusText||"Error",{status:t.status});t.text().then(function(e){n(null,{status:t.status,data:e})}).catch(n)}if(o){o=o(e,t);if(o instanceof Promise)return void o.then(r).catch(n)}("function"==typeof fetch?fetch:h)(e,t).then(r).catch(n)},P=!1;r.default=function(e,t,n,o){if("function"==typeof n&&(o=n,n=void 0),o=o||function(){},h&&0!==t.indexOf("file:")){var r=e,i=t,a=n,s=o,f=(r.queryStringParams&&(i=O(i,r.queryStringParams)),g({},"function"==typeof r.customHeaders?r.customHeaders():r.customHeaders)),u=("undefined"==typeof window&&void 0!==S&&void 0!==S.process&&S.process.versions&&S.process.versions.node&&(f["User-Agent"]="i18next-http-backend (node/".concat(S.process.version,"; ").concat(S.process.platform," ").concat(S.process.arch,")")),a&&(f["Content-Type"]="application/json"),"function"==typeof r.requestOptions?r.requestOptions(a):r.requestOptions),c=g({method:a?"POST":"GET",body:a?r.stringify(a):void 0,headers:f},P?{}:u),a="function"==typeof r.alternateFetch&&1<=r.alternateFetch.length?r.alternateFetch:void 0;try{j(i,c,s,a)}catch(e){if(!u||0===Object.keys(u).length||!e.message||e.message.indexOf("not implemented")<0)return s(e);try{Object.keys(u).forEach(function(e){delete c[e]}),j(i,c,s,a),P=!0}catch(e){s(e)}}}else if((0,m.hasXMLHttpRequest)()||"function"==typeof ActiveXObject){var f=e,r=t,u=n,l=o;u&&"object"===w(u)&&(u=O("",u).slice(1)),f.queryStringParams&&(r=O(r,f.queryStringParams));try{var d=b?new b:new v("MSXML2.XMLHTTP.3.0"),p=(d.open(u?"POST":"GET",r,1),f.crossDomain||d.setRequestHeader("X-Requested-With","XMLHttpRequest"),d.withCredentials=!!f.withCredentials,u&&d.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),d.overrideMimeType&&d.overrideMimeType("application/json"),f.customHeaders);if(p="function"==typeof p?p():p)for(var y in p)d.setRequestHeader(y,p[y]);d.onreadystatechange=function(){3e&&"function"==typeof e.then)(e))return e;return Promise.resolve(e)};var n=[],r=n.forEach,i=n.slice},{}],5:[function(e,t,n){},{}]},{},[2])(2)}); \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index b649494..d9999ae 100644 --- a/lib/index.js +++ b/lib/index.js @@ -74,7 +74,7 @@ class Backend { this.options.request(this.options, url, payload, (err, res) => { if (res && ((res.status >= 500 && res.status < 600) || !res.status)) return callback('failed loading ' + url + '; status code: ' + res.status, true /* retry */) if (res && res.status >= 400 && res.status < 500) return callback('failed loading ' + url + '; status code: ' + res.status, false /* no retry */) - if (!res && err && err.message && (err.message.indexOf('Failed to fetch') > -1 || (err.message.indexOf('failed') > -1 && err.message.indexOf('fetch') > -1))) return callback('failed loading ' + url + ': ' + err.message, true /* retry */) + if (!res && err && err.message && (err.message.toLowerCase().indexOf('failed') > -1 && (err.message.indexOf('fetch') > -1 || err.message.toLowerCase().indexOf('network') > -1))) return callback('failed loading ' + url + ': ' + err.message, true /* retry */) if (err) return callback(err, false) let ret, parseErr