MCPcopy
hub / github.com/ampproject/amphtml / fetchDocument

Function fetchDocument

examples/pwa/pwa.js:373–404  ·  view source on GitHub ↗

* @param {string} url * @return {!Promise<!Document>}

(url)

Source from the content-addressed store, hash-verified

371 * @return {!Promise<!Document>}
372 */
373function fetchDocument(url) {
374 return new Promise((resolve, reject) => {
375 const xhr = new XMLHttpRequest();
376 xhr.open('GET', url, true);
377 xhr.responseType = 'document';
378 xhr.setRequestHeader('Accept', 'text/html');
379 xhr.onreadystatechange = () => {
380 if (xhr.readyState < /* STATUS_RECEIVED */ 2) {
381 return;
382 }
383 if (xhr.status < 100 || xhr.status > 599) {
384 xhr.onreadystatechange = null;
385 reject(new Error(`Unknown HTTP status ${xhr.status}`));
386 return;
387 }
388 if (xhr.readyState == /* COMPLETE */ 4) {
389 if (xhr.responseXML) {
390 resolve(xhr.responseXML);
391 } else {
392 reject(new Error(`No xhr.responseXML`));
393 }
394 }
395 };
396 xhr.onerror = () => {
397 reject(new Error('Network failure'));
398 };
399 xhr.onabort = () => {
400 reject(new Error('Request aborted'));
401 };
402 xhr.send();
403 });
404}
405
406/**
407 * @param {string} url

Callers 1

navigateToMethod · 0.70

Calls 3

resolveFunction · 0.50
openMethod · 0.45
sendMethod · 0.45

Tested by

no test coverage detected