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}
    } + ); } };