[RN] Remove no longer needed fetch API fallback
This commit is contained in:
parent
531b638a8a
commit
c0f648b1ab
|
@ -1,7 +1,7 @@
|
||||||
/**
|
/**
|
||||||
* Loads a script from a specific URL. React Native cannot load a JS
|
* Loads a script from a specific URL. React Native cannot load a JS
|
||||||
* file/resource/URL via a <script> HTML element, so the implementation
|
* file/resource/URL via a <script> HTML element, so the implementation
|
||||||
* fetches the specified src as plain text (e.g. via XMLHttpRequest) and then
|
* fetches the specified src as plain text using fetch() and then
|
||||||
* evaluates the fetched string as JavaScript code (i.e. via the {@link eval}
|
* evaluates the fetched string as JavaScript code (i.e. via the {@link eval}
|
||||||
* function).
|
* function).
|
||||||
*
|
*
|
||||||
|
@ -10,35 +10,8 @@
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
export function loadScript(url) {
|
export function loadScript(url) {
|
||||||
let fetch;
|
|
||||||
const method = 'GET';
|
|
||||||
|
|
||||||
// Prefer the Fetch API. Apart from the fact that we're fetching the
|
|
||||||
// specified script as a static resource, the Fetch API provides more
|
|
||||||
// detailed errors.
|
|
||||||
if (typeof (fetch = window.fetch) === 'function') {
|
|
||||||
fetch = fetch(url, { method });
|
|
||||||
} else {
|
|
||||||
// Otherwise, fall back to the XMLHttpRequest API.
|
|
||||||
fetch
|
|
||||||
= new Promise(resolve => {
|
|
||||||
const xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xhr.responseType = 'text';
|
|
||||||
|
|
||||||
xhr.onreadystatechange = () => {
|
|
||||||
if (xhr.readyState === 4) {
|
|
||||||
resolve(xhr);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
xhr.open(method, url, /* async */ true);
|
|
||||||
xhr.send();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
fetch
|
fetch(url, { method: 'GET' })
|
||||||
.then(response => {
|
.then(response => {
|
||||||
switch (response.status) {
|
switch (response.status) {
|
||||||
case 200:
|
case 200:
|
||||||
|
|
Loading…
Reference in New Issue