From 8e25babd9ea1196f2f66dedc18618356da1f4335 Mon Sep 17 00:00:00 2001 From: winwiz1 Date: Sun, 24 Oct 2021 21:42:43 +1100 Subject: [PATCH] Client: Improve SSR --- client/src/components/ComponentB.tsx | 3 +- client/src/components/Navigation.tsx | 46 ++++++++++++++++-- client/src/entrypoints/first.tsx | 4 -- client/src/entrypoints/second.tsx | 4 -- .../src/utils/postprocess/postProcessSSR.ts | 47 +++++++------------ 5 files changed, 60 insertions(+), 44 deletions(-) diff --git a/client/src/components/ComponentB.tsx b/client/src/components/ComponentB.tsx index bab3b44..4b077f4 100644 --- a/client/src/components/ComponentB.tsx +++ b/client/src/components/ComponentB.tsx @@ -9,7 +9,6 @@ import { Helmet } from "react-helmet"; import { Header, Icon, Container, Menu } from "semantic-ui-react"; import { BaseComponent } from "./BaseComponent"; import { StructuredData } from "./StructuredData"; -import * as SPAs from "../../config/spa.config"; import { getTitle, getCanonical } from "../utils/misc"; const cssNav = css({ @@ -64,7 +63,7 @@ const Navigation: React.FC = _props => { Go back to - + First SPA = { menu: styles["menu"], }; +const pathArray = ["/", "/a", "/lighthouse", "/namelookup"]; + +const getIndex = () => { + if (isServer()) { + return -1; + } + const path = window.location.pathname; + return pathArray.findIndex((item) => item === path); +} + export const Navigation: React.FC = _props => { + React.useEffect(() => { + const idx = getIndex(); + const menuItem = document.querySelector(`div.menu>a:nth-child(${idx + 1})`); + menuItem?.classList.add("active"); + if (idx > 0) { + const menuItem = document.querySelector("div.menu>a:nth-child(1)"); + menuItem?.classList.remove("active"); + } + }, []); // run once upon initial rendering + return (