From 037fff08411632e1f71ac7e5947d074ebe7c1482 Mon Sep 17 00:00:00 2001 From: Jake Jarvis Date: Wed, 24 Nov 2021 18:02:58 -0500 Subject: [PATCH] fix window !== undefined checks --- assets/js/src/hits.js | 11 ++++------- assets/js/src/projects.js | 6 ++---- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/assets/js/src/hits.js b/assets/js/src/hits.js index 249619ab..f88cb3b9 100644 --- a/assets/js/src/hits.js +++ b/assets/js/src/hits.js @@ -32,11 +32,8 @@ const Counter = (props) => { ); }; -// don't continue if there isn't a span#meta-hits element on this page -const wrapper = document.querySelector("div#meta-hits-counter"); - -// page must have both span#meta-hits and canonical URL to enter -if (typeof window !== "undefined" && wrapper) { +// page must have a div#meta-hits-counter element to continue +if (typeof window !== "undefined" && document.querySelector("div#meta-hits-counter")) { // use to deduce a consistent identifier for this page const canonical = canonicalUrl({ normalize: true, @@ -48,7 +45,7 @@ if (typeof window !== "undefined" && wrapper) { }); // get path and strip beginning and ending forward slash - const slug = new URL(canonical).pathname.replace(/^\/|\/$/g, ""); + const slug = new URL(canonical).pathname.replace(/^\/|\/$/g, "") || "/"; - render(, wrapper); + render(, document.querySelector("div#meta-hits-counter")); } diff --git a/assets/js/src/projects.js b/assets/js/src/projects.js index 621951ff..ad218e54 100644 --- a/assets/js/src/projects.js +++ b/assets/js/src/projects.js @@ -99,9 +99,7 @@ const RepositoryCard = (repo) => ( ); // detect if these cards are wanted on this page (only /projects) -const wrapper = document.querySelector("div#github-cards"); - -if (typeof window !== "undefined" && wrapper) { +if (typeof window !== "undefined" && document.querySelector("div#github-cards")) { // dayjs plugins: https://day.js.org/docs/en/plugin/loading-into-nodejs dayjs.extend(dayjsAdvancedFormat); dayjs.extend(dayjsLocalizedFormat); @@ -111,5 +109,5 @@ if (typeof window !== "undefined" && wrapper) { // https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List dayjs.tz.setDefault("America/New_York"); - render(, wrapper); + render(, document.querySelector("div#github-cards")); }