From 1e382d6974890d6aa5bd8bb32fab89dcd40a178a Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Sun, 11 Jan 2026 22:57:37 +0100 Subject: [PATCH] fix: resolve giscus comments navigation error Fix NotFoundError when navigating away from blog posts with giscus comments. - Replace React-managed cleanup with manual innerHTML clearing - Add isClient state to prevent SSR hydration issues - Use figure/figcaption for images to avoid invalid HTML nesting - Add smart p component to detect block children and render as div The error occurred because giscus creates DOM elements that React doesn't track. Using innerHTML='' in cleanup prevents React reconciliation conflicts. Co-Authored-By: Claude Opus 4.5 --- components/blog/Comments.tsx | 49 ++++++++++++++++------------ components/blog/ServerMDXContent.tsx | 39 +++++++++++++++++----- 2 files changed, 60 insertions(+), 28 deletions(-) diff --git a/components/blog/Comments.tsx b/components/blog/Comments.tsx index f65c4a2..06b9ea3 100644 --- a/components/blog/Comments.tsx +++ b/components/blog/Comments.tsx @@ -1,21 +1,31 @@ 'use client'; -import { useEffect, useRef } from 'react'; +import { useRef, useEffect, useState } from 'react'; export default function Comments({ slug }: { slug: string }) { - const commentsRef = useRef(null); - - useEffect(() => { - // Capture ref value at effect start for cleanup - const container = commentsRef.current; + const containerRef = useRef(null); + const [isClient, setIsClient] = useState(false); - // Load Giscus + useEffect(() => { + setIsClient(true); + }, []); + + useEffect(() => { + if (!isClient) return; + + const container = containerRef.current; + if (!container) return; + + // Clear any existing content first + container.innerHTML = ''; + + // Create giscus script element const script = document.createElement('script'); script.src = 'https://giscus.app/client.js'; script.setAttribute('data-repo', 'g-but/botsmann-blog-content'); - script.setAttribute('data-repo-id', 'R_kgDOODOnUA'); // Replace with actual repo ID from Giscus setup + script.setAttribute('data-repo-id', 'R_kgDOODOnUA'); script.setAttribute('data-category', 'Blog Comments'); - script.setAttribute('data-category-id', 'DIC_kwDOODOnUM4CnkL1'); // Replace with actual category ID from Giscus setup + script.setAttribute('data-category-id', 'DIC_kwDOODOnUM4CnkL1'); script.setAttribute('data-mapping', 'pathname'); script.setAttribute('data-strict', '0'); script.setAttribute('data-reactions-enabled', '1'); @@ -27,24 +37,23 @@ export default function Comments({ slug }: { slug: string }) { script.crossOrigin = 'anonymous'; script.async = true; - if (container) { - container.appendChild(script); - } + container.appendChild(script); return () => { + // Manually clean up all children using innerHTML to prevent React reconciliation errors + // This runs before React tries to reconcile the DOM if (container) { - const giscusFrame = container.querySelector('iframe.giscus-frame'); - if (giscusFrame) { - container.removeChild(giscusFrame); - } + container.innerHTML = ''; } }; - }, [slug]); - + }, [slug, isClient]); + return (

Comments

-
+
+ {!isClient &&
Loading comments...
} +
); -} \ No newline at end of file +} diff --git a/components/blog/ServerMDXContent.tsx b/components/blog/ServerMDXContent.tsx index dfee605..5f4b11a 100644 --- a/components/blog/ServerMDXContent.tsx +++ b/components/blog/ServerMDXContent.tsx @@ -1,3 +1,4 @@ +import React from 'react'; import { MDXRemote } from 'next-mdx-remote/rsc'; import remarkGfm from 'remark-gfm'; import rehypeSlug from 'rehype-slug'; @@ -19,10 +20,31 @@ const ServerMDXComponents = {

), - // Text elements - p: (props: React.HTMLAttributes) => ( -

- ), + // Text elements - handle block elements that MDX might wrap in

+ p: ({ children, ...props }: React.HTMLAttributes) => { + // Check if children contain block-level elements (images wrapped in figure) + // If so, render as a div to avoid invalid HTML nesting + const hasBlockChild = React.Children.toArray(children).some((child) => { + if (React.isValidElement(child)) { + // Check for figure, div, or other block elements + const type = child.type; + if (typeof type === 'string' && ['figure', 'div', 'section'].includes(type)) { + return true; + } + // Check for custom components that render block elements + if (typeof type === 'function' || typeof type === 'object') { + return true; + } + } + return false; + }); + + if (hasBlockChild) { + return

{children}
; + } + + return

{children}

; + }, ul: (props: React.HTMLAttributes) => (
    ), @@ -107,7 +129,7 @@ export default function ServerMDXContent({ content, slug }: ServerMDXContentProp const { src, alt } = imgProps; if (!src) { - return
    Image source missing
    ; + return Image source missing; } let imageSrc = src; @@ -118,8 +140,9 @@ export default function ServerMDXContent({ content, slug }: ServerMDXContentProp imageSrc = `https://raw.githubusercontent.com/g-but/botsmann-blog-content/main/posts/${slug}/${imagePath}`; } + // Use figure/figcaption for semantic HTML that avoids

    nesting issues return ( -

    +
    {alt - {alt &&

    {alt}

    } -
    + {alt &&
    {alt}
    } + ); } };