Here’s what I have in my next app. It works how I mentioned previously - have to press refresh. Lemme know if it works.
Sometimes i have ot refresh a few times or wait for a while then refresh to get pas tthe ‘loading discussion’ message
<DiscourseComments
discourseUrl="https://discourseurl.com/"
discourseEmbedUrl={embedUrl}
username="system"
/>
component:
"use client"
import { useEffect, useRef } from "react"
/**
* A React component to embed Discourse comments on a website
*
* @param {Object} props - Component props
* @param {string} props.discourseUrl - The URL of your Discourse instance (including trailing slash)
* @param {string} [props.discourseEmbedUrl] - The URL of the current page (for auto topic creation)
* @param {number} [props.topicId] - ID of an existing Discourse topic (alternative to discourseEmbedUrl)
* @param {string} [props.username] - Username for topic creation
* @param {string} [props.className] - CSS class name to add to the embed
* @param {string} [props.referrerPolicy] - Referrer policy for the iframe
*/
export default function DiscourseComments({
discourseUrl,
discourseEmbedUrl,
topicId,
username,
className,
referrerPolicy = "no-referrer-when-downgrade",
}) {
const initialized = useRef(false)
useEffect(() => {
// Prevent duplicate initialization
if (initialized.current) return
initialized.current = true
// Validate required props
if (!discourseUrl) {
console.error("DiscourseComments: discourseUrl is required")
return
}
if (!discourseEmbedUrl && !topicId) {
console.error("DiscourseComments: Either discourseEmbedUrl or topicId must be provided")
return
}
// Configure the Discourse embed
const config = {
discourseUrl,
discourseReferrerPolicy: referrerPolicy,
}
if (discourseEmbedUrl) {
config.discourseEmbedUrl = discourseEmbedUrl
}
if (topicId) {
config.topicId = topicId
}
if (className) {
config.className = className
}
// Set the global DiscourseEmbed object
window.DiscourseEmbed = config
// Load the Discourse embed script
const script = document.createElement("script")
script.type = "text/javascript"
script.async = true
script.src = `${discourseUrl}javascripts/embed.js`
document.head.appendChild(script)
// Cleanup
return () => {
document.head.removeChild(script)
delete window.DiscourseEmbed
}
}, [discourseUrl, discourseEmbedUrl, topicId, className, referrerPolicy])
return (
<>
<div id="discourse-comments"></div>
{username && <meta name="discourse-username" content={username} />}
</>
)
}