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.
This commit is contained in:
George Cushen 2020-12-19 15:32:48 +00:00
commit 769d5f0ad1
2 changed files with 19 additions and 12 deletions

View file

@ -5,6 +5,8 @@
* Core JS functions and initialization. * Core JS functions and initialization.
**************************************************/ **************************************************/
import {hugoEnvironment} from '@params';
import { import {
canChangeTheme, canChangeTheme,
changeThemeModeClick, changeThemeModeClick,
@ -13,6 +15,7 @@ import {
renderThemeVariation renderThemeVariation
} from './wowchemy-theming'; } from './wowchemy-theming';
console.debug(`Environment: ${hugoEnvironment}`)
/* --------------------------------------------------------------------------- /* ---------------------------------------------------------------------------
* Responsive scrolling for URL hashes. * Responsive scrolling for URL hashes.
@ -32,7 +35,7 @@ function getNavBarHeight() {
* If it exists on current page, scroll to it responsively. * 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. * 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. // 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). // 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; target = (typeof target === 'undefined' || typeof target === 'object') ? decodeURIComponent(window.location.hash) : target;
@ -305,13 +308,15 @@ function initMap() {
* --------------------------------------------------------------------------- */ * --------------------------------------------------------------------------- */
function printLatestRelease(selector, repo) { function printLatestRelease(selector, repo) {
$.getJSON('https://api.github.com/repos/' + repo + '/tags').done(function (json) { if (hugoEnvironment === 'production') {
let release = json[0]; $.getJSON('https://api.github.com/repos/' + repo + '/tags').done(function (json) {
$(selector).append(' ' + release.name); let release = json[0];
}).fail(function (jqxhr, textStatus, error) { $(selector).append(' ' + release.name);
let err = textStatus + ", " + error; }).fail(function (jqxhr, textStatus, error) {
console.log("Request Failed: " + err); let err = textStatus + ", " + error;
}); console.log("Request Failed: " + err);
});
}
} }
/* --------------------------------------------------------------------------- /* ---------------------------------------------------------------------------
@ -400,7 +405,7 @@ function fixMermaid() {
} }
// Get an element's siblings. // Get an element's siblings.
function getSiblings (elem) { function getSiblings(elem) {
// Filter out itself. // Filter out itself.
return Array.prototype.filter.call(elem.parentNode.children, function (sibling) { return Array.prototype.filter.call(elem.parentNode.children, function (sibling) {
return sibling !== elem; return sibling !== elem;
@ -537,7 +542,7 @@ $(window).on('load', function () {
// Hook to perform actions once all Isotope instances have loaded. // Hook to perform actions once all Isotope instances have loaded.
function incrementIsotopeCounter() { function incrementIsotopeCounter() {
isotopeCounter++; isotopeCounter++;
if ( isotopeCounter === isotopeInstancesCount ) { if (isotopeCounter === isotopeInstancesCount) {
console.debug(`All Portfolio Isotope instances loaded.`); console.debug(`All Portfolio Isotope instances loaded.`);
// Once all Isotope instances and their images have loaded, scroll to hash (if set). // 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. // 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. // Print latest version of GitHub projects.
let githubReleaseSelector = '.js-github-release'; let githubReleaseSelector = '.js-github-release';
if ($(githubReleaseSelector).length > 0) if ($(githubReleaseSelector).length > 0) {
printLatestRelease(githubReleaseSelector, $(githubReleaseSelector).data('repo')); printLatestRelease(githubReleaseSelector, $(githubReleaseSelector).data('repo'));
}
// On search icon click toggle search dialog. // On search icon click toggle search dialog.
$('.js-search').click(function (e) { $('.js-search').click(function (e) {

View file

@ -155,7 +155,8 @@
{{ $js_comment := printf "/* Wowchemy v%s | https://wowchemy.com/ */\n" site.Data.wowchemy.version }} {{ $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_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_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_academic_search := resources.Get "js/wowchemy-search.js" }}
{{ $js_algolia_search := resources.Get "js/algolia-search.js" }} {{ $js_algolia_search := resources.Get "js/algolia-search.js" }}
{{ $js_bootstrap := resources.Get "js/_vendor/bootstrap.bundle.js" }} {{ $js_bootstrap := resources.Get "js/_vendor/bootstrap.bundle.js" }}