From 769d5f0ad194d6b6fe64c788a5cbefb6c6691f62 Mon Sep 17 00:00:00 2001 From: George Cushen Date: Sat, 19 Dec 2020 15:32:48 +0000 Subject: [PATCH] feat: if using GitHub API, reduce requests in dev env For sites using the `.js-github-release` feature to get the latest release of their project, prevent sending request to GitHub everytime Hugo Server reloads pages. --- wowchemy/assets/js/wowchemy.js | 28 ++++++++++++++++---------- wowchemy/layouts/partials/site_js.html | 3 ++- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/wowchemy/assets/js/wowchemy.js b/wowchemy/assets/js/wowchemy.js index 283ef8fa..241a09f9 100644 --- a/wowchemy/assets/js/wowchemy.js +++ b/wowchemy/assets/js/wowchemy.js @@ -5,6 +5,8 @@ * Core JS functions and initialization. **************************************************/ +import {hugoEnvironment} from '@params'; + import { canChangeTheme, changeThemeModeClick, @@ -13,6 +15,7 @@ import { renderThemeVariation } from './wowchemy-theming'; +console.debug(`Environment: ${hugoEnvironment}`) /* --------------------------------------------------------------------------- * Responsive scrolling for URL hashes. @@ -32,7 +35,7 @@ function getNavBarHeight() { * If it exists on current page, scroll to it responsively. * If `target` argument omitted (e.g. after event), assume it's the window's hash. */ -function scrollToAnchor(target, duration=600) { +function scrollToAnchor(target, duration = 600) { // If `target` is undefined or HashChangeEvent object, set it to window's hash. // Decode the hash as browsers can encode non-ASCII characters (e.g. Chinese symbols). target = (typeof target === 'undefined' || typeof target === 'object') ? decodeURIComponent(window.location.hash) : target; @@ -305,13 +308,15 @@ function initMap() { * --------------------------------------------------------------------------- */ function printLatestRelease(selector, repo) { - $.getJSON('https://api.github.com/repos/' + repo + '/tags').done(function (json) { - let release = json[0]; - $(selector).append(' ' + release.name); - }).fail(function (jqxhr, textStatus, error) { - let err = textStatus + ", " + error; - console.log("Request Failed: " + err); - }); + if (hugoEnvironment === 'production') { + $.getJSON('https://api.github.com/repos/' + repo + '/tags').done(function (json) { + let release = json[0]; + $(selector).append(' ' + release.name); + }).fail(function (jqxhr, textStatus, error) { + let err = textStatus + ", " + error; + console.log("Request Failed: " + err); + }); + } } /* --------------------------------------------------------------------------- @@ -400,7 +405,7 @@ function fixMermaid() { } // Get an element's siblings. -function getSiblings (elem) { +function getSiblings(elem) { // Filter out itself. return Array.prototype.filter.call(elem.parentNode.children, function (sibling) { return sibling !== elem; @@ -537,7 +542,7 @@ $(window).on('load', function () { // Hook to perform actions once all Isotope instances have loaded. function incrementIsotopeCounter() { isotopeCounter++; - if ( isotopeCounter === isotopeInstancesCount ) { + if (isotopeCounter === isotopeInstancesCount) { console.debug(`All Portfolio Isotope instances loaded.`); // Once all Isotope instances and their images have loaded, scroll to hash (if set). // Prevents scrolling to the wrong location due to the dynamic height of Isotope instances. @@ -596,8 +601,9 @@ $(window).on('load', function () { // Print latest version of GitHub projects. let githubReleaseSelector = '.js-github-release'; - if ($(githubReleaseSelector).length > 0) + if ($(githubReleaseSelector).length > 0) { printLatestRelease(githubReleaseSelector, $(githubReleaseSelector).data('repo')); + } // On search icon click toggle search dialog. $('.js-search').click(function (e) { diff --git a/wowchemy/layouts/partials/site_js.html b/wowchemy/layouts/partials/site_js.html index 5c698e52..f6d1de10 100644 --- a/wowchemy/layouts/partials/site_js.html +++ b/wowchemy/layouts/partials/site_js.html @@ -155,7 +155,8 @@ {{ $js_comment := printf "/* Wowchemy v%s | https://wowchemy.com/ */\n" site.Data.wowchemy.version }} {{ $js_bundle_head := $js_comment | resources.FromString "js/bundle-head.js" }} {{ $js_linebreak := "\n" | resources.FromString "js/linebreak.js" }}{{/* Fix no line break after Bootstrap JS causing error. */}} - {{ $js_academic := resources.Get "js/wowchemy.js" | js.Build }} + {{ $js_params := dict "hugoEnvironment" hugo.Environment }} + {{ $js_academic := resources.Get "js/wowchemy.js" | js.Build (dict "params" $js_params) }} {{ $js_academic_search := resources.Get "js/wowchemy-search.js" }} {{ $js_algolia_search := resources.Get "js/algolia-search.js" }} {{ $js_bootstrap := resources.Get "js/_vendor/bootstrap.bundle.js" }}