Skip to content

Commit 56830df

Browse files
committed
JSONP fallback to fetch
1 parent 085555b commit 56830df

2 files changed

Lines changed: 43 additions & 29 deletions

File tree

‎src/models/SearchCodeModel.js‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,34 @@ class SearchCodeModel extends BaseModel {
7171
const langParams = lang.length ? ('&lan=' + lang.join(',').split(',').join('&lan=')) : '';
7272
const qParams = q.replace(' ', '+');
7373
const url = `//searchcode.com/api/jsonp_codesearch_I/?callback=?&q=${qParams}&p=${page}&per_page=42${langParams}`;
74+
const done = data => {
75+
const cdata = {
76+
searchValue: val,
77+
page: page,
78+
variableList: [...this._data.variableList, this._parseVariableList(data.results, q)],
79+
searchLang: lang,
80+
suggestion: suggestion,
81+
isZH: isZH || this.isZH
82+
};
83+
this.update(cdata);
84+
this._variableListStore.save(cacheId, cdata);
85+
};
7486
val && JSONP(url, {callbackName: 'searchcodeRequestVariableCallback'})
75-
.then(data => {
76-
const cdata = {
77-
searchValue: val,
78-
page: page,
79-
variableList: [...this._data.variableList, this._parseVariableList(data.results, q)],
80-
searchLang: lang,
81-
suggestion: suggestion,
82-
isZH: isZH || this.isZH
83-
};
84-
this.update(cdata);
85-
this._variableListStore.save(cacheId, cdata);
86-
}).catch(err => {
87-
this.update({
88-
searchValue: val,
89-
page: page,
90-
variableList: [...this.variableList, []],
91-
searchLang: lang,
92-
suggestion: suggestion,
93-
isZH: isZH || this.isZH
94-
});
87+
.then(done).catch(() => {
88+
// fallback to fetch
89+
fetch(`//searchcode.com/api/codesearch_I/?q=${qParams}&p=${page}&per_page=42${langParams}`)
90+
.then(res => res.json())
91+
.then(done)
92+
.catch(() => {
93+
this.update({
94+
searchValue: val,
95+
page: page,
96+
variableList: [...this.variableList, []],
97+
searchLang: lang,
98+
suggestion: suggestion,
99+
isZH: isZH || this.isZH
100+
});
101+
});
95102
});
96103
}
97104

‎src/utils/JSONP.js‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@ const JSONP = (url, options) => {
55
let timer = 0;
66
let script = document.createElement('script');
77
const callbackName = options.callbackName || `__jsonp_${Date.now()}_callback`;
8-
window[callbackName] = (...args) => {
8+
url = url.replace('=?', `=${callbackName}${options.nocache ? ('&_=' + Date.now()) : ''}`);
9+
const done = () => {
910
window.clearTimeout(timer);
10-
document.body.removeChild(script);
11-
resolve(...args);
11+
try {document.head.removeChild(script);} catch (e) {}
1212
window[callbackName] = null;
1313
};
14-
setTimeout(() => {
15-
document.body.removeChild(script);
14+
const onerror = () => {
15+
window.removeEventListener('error', onerror);
16+
done();
1617
reject();
17-
window[callbackName] = null;
18-
}, 5 * 60 * 1000); // timeout in 5 min
19-
script.src = url.replace('=?', `=${callbackName}${options.nocache ? ('&_=' + Date.now()) : ''}`);
20-
document.body.appendChild(script);
18+
};
19+
window[callbackName] = (...args) => {
20+
done();
21+
resolve(...args);
22+
};
23+
timer = setTimeout(onerror, 5 * 60 * 1000); // timeout in 5 min
24+
window.addEventListener('error', onerror);
25+
script.onerror = onerror;
26+
script.src = url;
27+
document.head.appendChild(script);
2128
});
2229
}
2330
export default JSONP;

0 commit comments

Comments
 (0)