Skip to content

Commit 9f53daf

Browse files
committed
use Fetch is available
1 parent c359e85 commit 9f53daf

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

utilities/warning/url-exists.js

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,58 @@ 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;
16+
if (typeof fetch === 'function') {
17+
// 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.
18+
urlExists = function (control, url, comment) {
19+
if (
20+
!hasExecuted &&
21+
!hasWarned[`${control}-path`] &&
22+
typeof window !== 'undefined' &&
23+
process.env.NODE_ENV !== 'test'
24+
) {
25+
fetch(url).then((res) => {
26+
hasExecuted = true;
27+
if (res.status === 404) {
28+
const additionalComment = comment ? ` ${comment}` : '';
29+
/* eslint-disable max-len */
30+
warning(
31+
!url,
32+
`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}`
33+
);
34+
/* eslint-enable max-len */
35+
hasWarned[`${control}-path`] = !!url;
36+
}
37+
});
38+
}
39+
};
40+
} else {
41+
// 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.
42+
urlExists = function (control, url, comment) {
43+
if (
44+
!hasExecuted &&
45+
!hasWarned[`${control}-path`] &&
46+
typeof window !== 'undefined' &&
47+
XMLHttpRequest &&
48+
process.env.NODE_ENV !== 'test'
49+
) {
50+
const http = new XMLHttpRequest();
51+
http.open('GET', url, false);
52+
http.send();
53+
hasExecuted = true;
2954

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;
55+
if (http.status === 404) {
56+
const additionalComment = comment ? ` ${comment}` : '';
57+
/* eslint-disable max-len */
58+
warning(
59+
!url,
60+
`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}`
61+
);
62+
/* eslint-enable max-len */
63+
hasWarned[`${control}-path`] = !!url;
64+
}
3965
}
40-
}
41-
};
66+
};
67+
}
4268
}
4369

4470
export default urlExists;

0 commit comments

Comments
 (0)