From cae8a8ffe2292147a6f56839ec57069b94f6cc77 Mon Sep 17 00:00:00 2001 From: Matt Ickstadt Date: Thu, 14 Jun 2018 13:09:20 -0500 Subject: [PATCH] Only fetch crates list on pages with playpens --- src/theme/book.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/theme/book.js b/src/theme/book.js index 521f1371..6d489981 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -18,13 +18,23 @@ function playpen_text(playpen) { (function codeSnippets() { // Hide Rust code lines prepended with a specific character var hiding_character = "#"; - var request = fetch("https://play.rust-lang.org/meta/crates", { - headers: { - 'Content-Type': "application/json", - }, - method: 'POST', - mode: 'cors', - }); + + var playpens = Array.from(document.querySelectorAll(".playpen")); + if (playpens.length > 0) { + fetch("https://play.rust-lang.org/meta/crates", { + headers: { + 'Content-Type': "application/json", + }, + method: 'POST', + mode: 'cors', + }) + .then(response => response.json()) + .then(response => { + // get list of crates available in the rust playground + let playground_crates = response.crates.map(item => item["id"]); + playpens.forEach(block => handle_crate_list_update(block, playground_crates)); + }); + } function handle_crate_list_update(playpen_block, playground_crates) { // update the play buttons after receiving the response @@ -274,17 +284,6 @@ function playpen_text(playpen) { }); } }); - - request - .then(function (response) { return response.json(); }) - .then(function (response) { - // get list of crates available in the rust playground - let playground_crates = response.crates.map(function (item) { return item["id"]; }); - Array.from(document.querySelectorAll(".playpen")).forEach(function (block) { - handle_crate_list_update(block, playground_crates); - }); - }); - })(); (function themes() {