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 <noreply@anthropic.com>
This commit is contained in:
g-butandClaude Opus 4.5 committed 2026-01-11 22:57:37 +01:00
1 parent 1f9a6a4973
commit 1e382d6974
2 files changed
+60 -28

No files matched your search

+29 -20
View File
@@ -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<HTMLDivElement>(null);
useEffect(() => {
// Capture ref value at effect start for cleanup
const container = commentsRef.current;
const containerRef = useRef<HTMLDivElement>(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 (
<div className="mt-16 pt-8 border-t border-gray-200">
<h2 className="text-2xl font-semibold text-gray-900 mb-8">Comments</h2>
<div ref={commentsRef} />
<div ref={containerRef} className="giscus-container">
{!isClient && <div className="text-gray-500">Loading comments...</div>}
</div>
</div>
);
}
}
+31 -8
View File
@@ -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 = {
<h3 className="mb-4 mt-6 text-xl font-semibold text-gray-900" {...props} />
),
// Text elements
p: (props: React.HTMLAttributes<HTMLParagraphElement>) => (
<p className="mb-4 text-gray-600" {...props} />
),
// Text elements - handle block elements that MDX might wrap in <p>
p: ({ children, ...props }: React.HTMLAttributes<HTMLParagraphElement>) => {
// 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 <div className="mb-4 text-gray-600" {...props}>{children}</div>;
}
return <p className="mb-4 text-gray-600" {...props}>{children}</p>;
},
ul: (props: React.HTMLAttributes<HTMLUListElement>) => (
<ul className="mb-4 list-disc pl-6 text-gray-600" {...props} />
),
@@ -107,7 +129,7 @@ export default function ServerMDXContent({ content, slug }: ServerMDXContentProp
const { src, alt } = imgProps;
if (!src) {
return <div className="my-8 p-4 bg-red-50 text-red-500">Image source missing</div>;
return <span className="block my-8 p-4 bg-red-50 text-red-500">Image source missing</span>;
}
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 <p> nesting issues
return (
<div className="my-8">
<figure className="my-8">
<Image
src={imageSrc}
alt={alt || ''}
@@ -127,8 +150,8 @@ export default function ServerMDXContent({ content, slug }: ServerMDXContentProp
height={450}
className="rounded-lg"
/>
{alt && <p className="mt-2 text-sm text-gray-500 italic">{alt}</p>}
</div>
{alt && <figcaption className="mt-2 text-sm text-gray-500 italic">{alt}</figcaption>}
</figure>
);
}
};