From 7c7ae7d7ccff08f976c3e454afde1efbbaac5a3e Mon Sep 17 00:00:00 2001 From: petrkucerak Date: Mon, 19 Feb 2024 15:41:43 +0100 Subject: [PATCH] Use dynamic loading navigation to prevent agresive caching --- components/utils/nav-buttons.js | 77 +++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/components/utils/nav-buttons.js b/components/utils/nav-buttons.js index dfd88f5..c45af54 100644 --- a/components/utils/nav-buttons.js +++ b/components/utils/nav-buttons.js @@ -1,4 +1,5 @@ import Link from "next/link"; +import { useEffect } from "react"; function formateDayNum(num) { if (num < 10) return `0${num}`; @@ -6,44 +7,56 @@ function formateDayNum(num) { } export default function NavButtons({ posts }) { - // const now = new Date("2022-11-28"); // testing date - const now = new Date(); - const nowDate = `${now.getFullYear()}-${formateDayNum( - now.getMonth() + 1 - )}-${formateDayNum(now.getDate())}`; - - const yesterday = new Date(now.setDate(now.getDate() - 1)); - const yesterdayDate = `${yesterday.getFullYear()}-${formateDayNum( - yesterday.getMonth() + 1 - )}-${formateDayNum(yesterday.getDate())}`; - - const tomorrow = new Date(now.setDate(now.getDate() + 2)); - const tomorrowDate = `${tomorrow.getFullYear()}-${formateDayNum( - tomorrow.getMonth() + 1 - )}-${formateDayNum(tomorrow.getDate())}`; - - // console.log(yesterdayDate, nowDate, tomorrowDate); - let yesterdayStatus, nowStatus, tomorrowStatus; - yesterdayStatus = nowStatus = tomorrowStatus = "hidden"; - - // console.log(tomorrowDate); - - posts.map((post) => { - if (nowDate === post.slug) nowStatus = ""; - if (yesterdayDate === post.slug) yesterdayStatus = ""; - if (tomorrowDate === post.slug) tomorrowStatus = ""; + useEffect(() => { + // const now = new Date("2022-11-28"); // testing date + const now = new Date(); + const nowDate = `${now.getFullYear()}-${formateDayNum( + now.getMonth() + 1 + )}-${formateDayNum(now.getDate())}`; + + const yesterday = new Date(now.setDate(now.getDate() - 1)); + const yesterdayDate = `${yesterday.getFullYear()}-${formateDayNum( + yesterday.getMonth() + 1 + )}-${formateDayNum(yesterday.getDate())}`; + + const tomorrow = new Date(now.setDate(now.getDate() + 2)); + const tomorrowDate = `${tomorrow.getFullYear()}-${formateDayNum( + tomorrow.getMonth() + 1 + )}-${formateDayNum(tomorrow.getDate())}`; + + // console.log(yesterdayDate, nowDate, tomorrowDate); + let yesterdayStatus, nowStatus, tomorrowStatus; + yesterdayStatus = nowStatus = tomorrowStatus = "hidden"; + + posts.map((post) => { + if (nowDate === post.slug) nowStatus = ""; + if (yesterdayDate === post.slug) yesterdayStatus = ""; + if (tomorrowDate === post.slug) tomorrowStatus = ""; + }); + + const yesterdayEl = document.getElementById("yesterdayEl"); + const todayEl = document.getElementById("todayEl"); + const tomorowEl = document.getElementById("tomorowEl"); + + yesterdayEl.setAttribute("href", `/den/${yesterdayDate}`); + todayEl.setAttribute("href", `/den/${nowDate}`); + tomorowEl.setAttribute("href", `/den/${tomorrowDate}`); + + yesterdayEl.setAttribute("class", yesterdayStatus); + todayEl.setAttribute("class", nowStatus); + tomorowEl.setAttribute("class", tomorrowStatus); }); return (
- + - - + + - - + + - +
); }