Skip to content

Commit ec89933

Browse files
Merge pull request #1424 from celador/1423-synchronous-http-request
Use Fetch for url-exists, if available
2 parents e11c4e5 + fb2ec27 commit ec89933

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

utilities/warning/url-exists.js

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,43 @@ if (process.env.NODE_ENV !== 'production') {
1313
const hasWarned = {};
1414
let hasExecuted;
1515

16-
// Using XMLHttpRequest can cause problems in non-browser environments. This should be completely removed in production environment and should not execute in a testing environment.
17-
urlExists = function (control, url, comment) {
18-
if (
19-
!hasExecuted &&
20-
!hasWarned[`${control}-path`] &&
21-
typeof window !== 'undefined' &&
22-
XMLHttpRequest &&
23-
process.env.NODE_ENV !== 'test'
24-
) {
25-
const http = new XMLHttpRequest();
26-
http.open('GET', url, false);
27-
http.send();
28-
hasExecuted = true;
29-
30-
if (http.status === 404) {
31-
const additionalComment = comment ? ` ${comment}` : '';
32-
/* eslint-disable max-len */
33-
warning(
34-
!url,
35-
`The icon asset was not found at ${url}. Make sure the path to the icon asset is correct. You can set the icon path by importing the IconSettings component, \`<IconSettings iconPath=[/assets/icons]>\` from \`components/iconSettings\`, and wrap that component around your entire app or around individual components using icons. If you are using the \`<Icon>\` component, you can also pass the url to \`this.props.path\`.${additionalComment}`
36-
);
37-
/* eslint-enable max-len */
38-
hasWarned[`${control}-path`] = !!url;
39-
}
16+
const warn = (control, url, comment) => (res) => {
17+
hasExecuted = true;
18+
if (res.status === 404) {
19+
const additionalComment = comment ? ` ${comment}` : '';
20+
/* eslint-disable max-len */
21+
warning(
22+
!url,
23+
`The icon asset was not found at ${url}. Make sure the path to the icon asset is correct. You can set the icon path by importing the IconSettings component, \`<IconSettings iconPath=[/assets/icons]>\` from \`components/iconSettings\`, and wrap that component around your entire app or around individual components using icons. If you are using the \`<Icon>\` component, you can also pass the url to \`this.props.path\`.${additionalComment}`
24+
);
25+
/* eslint-enable max-len */
26+
hasWarned[`${control}-path`] = !!url;
4027
}
4128
};
29+
30+
const shouldWarn = (control) =>
31+
!hasExecuted &&
32+
!hasWarned[`${control}-path`] &&
33+
typeof window !== 'undefined' &&
34+
process.env.NODE_ENV !== 'test';
35+
36+
if (typeof fetch === 'function') {
37+
urlExists = function (control, url, comment) {
38+
if (shouldWarn(control)) {
39+
fetch(url).then(warn(control, url, comment));
40+
}
41+
};
42+
} else {
43+
// Using XMLHttpRequest can cause problems in non-browser environments. This should be completely removed in production environment and should not execute in a testing environment.
44+
urlExists = function (control, url, comment) {
45+
if (shouldWarn(control) && XMLHttpRequest) {
46+
const http = new XMLHttpRequest();
47+
http.open('GET', url, false);
48+
http.send();
49+
warn(control, url, comment)(http);
50+
}
51+
};
52+
}
4253
}
4354

4455
export default urlExists;

0 commit comments

Comments
 (0)