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

Function getAmpConfig

tools/experiments/experiments.js:379–405  ·  view source on GitHub ↗

* Loads the AMP_CONFIG objects from whatever the v0.js is that the user has * (depends on whether they opted into one of the pre-release channels or into a * specific RTV) so that experiment state can reflect the default activated * experiments. * @return {Promise } the active AMP_CONFIG, p

()

Source from the content-addressed store, hash-verified

377 * @return {Promise<JSON>} the active AMP_CONFIG, parsed as a JSON object
378 */
379function getAmpConfig() {
380 const deferred = new Deferred();
381 const {promise, reject, resolve} = deferred;
382 const xhr = new XMLHttpRequest();
383 xhr.addEventListener('load', () => {
384 resolve(xhr.responseText);
385 });
386 xhr.addEventListener('error', () => {
387 reject(new Error(xhr.statusText));
388 });
389 // Cache bust, so we immediately reflect cookie changes.
390 xhr.open('GET', '/v0.js?' + Math.random(), true);
391 xhr.send(null);
392 return promise
393 .then((text) => {
394 const match = text.match(/self\.AMP_CONFIG=(\{.+?\})/);
395 if (!match) {
396 throw new Error("Can't find AMP_CONFIG in: " + text);
397 }
398 // Setting global var to make standard experiment code just work.
399 return (self.AMP_CONFIG = JSON.parse(match[1]));
400 })
401 .catch((error) => {
402 console./*OK*/ error('Error fetching AMP_CONFIG', error);
403 return {};
404 });
405}
406
407// Start up.
408getAmpConfig().then(() => {

Callers 1

experiments.jsFile · 0.70

Calls 8

catchMethod · 0.80
resolveFunction · 0.50
addEventListenerMethod · 0.45
openMethod · 0.45
sendMethod · 0.45
thenMethod · 0.45
parseMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected