From b9ffc410fe614ab0c655c5ab554c28201687f44f Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Fri, 24 Nov 2023 22:06:10 +0100 Subject: [PATCH] fix: NextJS errors on difference between server-render and client-render (#22) This is because linkText is different on server (no "window") and client. Fix that by using a React hook. --- src/ShipFit/FitLink.tsx | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ShipFit/FitLink.tsx b/src/ShipFit/FitLink.tsx index 7a1e8d5..8661dc5 100644 --- a/src/ShipFit/FitLink.tsx +++ b/src/ShipFit/FitLink.tsx @@ -4,12 +4,21 @@ import { useEveShipFitLink } from "../EveShipFitLink"; import styles from "./ShipFit.module.css"; +const useIsRemoteViewer = () => { + const [remote, setRemote] = React.useState(true); + + React.useEffect(() => { + if (typeof window !== "undefined") { + setRemote(window.location.hostname !== "eveship.fit"); + } + }, []); + + return remote; +} + export const FitLink = () => { const link = useEveShipFitLink(); - - /* Detect if the fit is loaded on https://eveship.fit */ - const isEveShipFit = typeof window !== "undefined" && window.location.hostname === "eveship.fit"; - const linkText = isEveShipFit ? "link to fit" : "open on eveship.fit"; + const linkText = useIsRemoteViewer() ? "open on eveship.fit" : "link to fit"; return