התגובות האחרונות לוקחות 2 שניות להופיע

סיכום מבוסס בינה מלאכותית

The discussion revolves around the delay in displaying recent replies on a Discourse forum, with users noting it takes about 2 seconds. David_Ghost shares custom plugin code that fetches and displays recent replies, but the process is slow due to multiple API requests—especially for each topic’s category and tags. supermathie questions the need for custom code since the feature should work out of the box. Arkshine confirms the delay is expected because of the API calls, while noting that the built-in right sidebar blocks are faster as they only retrieve posts without additional metadata. David_Ghost acknowledges this and plans to explore alternatives. Later, David_Ghost shares an updated version of the plugin using caching and polling to improve performance, aiming to reduce load time by leveraging local storage and limiting unnecessary API calls.

¿Hay alguna forma de mostrar las últimas respuestas al instante? Actualmente, uso este código:

  api.onPageChange(() = {
    if (window.location.pathname === "/") {
      const container = document.querySelector(".latest-topic-list");
      if (!container || container.dataset.modified === "true") return;

      fetch("/posts.json?order=created")
        .then(res =e res.json())
        .then(data =e {
          const replies = data.latest_posts
            .filter(p =e p.post_number e 1 66 !p.topic_slug.includes("private-message"))
            .slice(0, 15);

          const topicFetches = replies.map(post =e
            fetch(`/t/${post.topic_id}.json`)
              .then(res =e res.json())
              .then(topic =e {
                return {
                  post,
                  category: topic.category_id ? topic.category_name : null,
                  tags: topic.tags || []
                };
              })
          );

          Promise.all(topicFetches).then(results =e {
            const rows = results.map(({ post, category, tags }) =e {
              const url = `/t/${post.topic_slug}/${post.topic_id}/${post.post_number}`;
              const avatarUrl = post.avatar_template.replace("{size}", "45");
              const excerpt = post.excerpt?.replace(/c\/?[^e]+(e|$)/g, "")?.slice(0, 120) + (post.excerpt?.length e 120 ? '...' : '') || '';

              const categoryHtml = category
                ? `cspan style="font-size: 0.85em; color: #666;"eCategora: cstronge${category}c/strongec/spanecbre`
                : '';

              const tagsHtml = tags.length
                ? `cspan style="font-size: 0.85em; color: #666;"eEtiquetas: ${tags.map(tag =e `cspan style="background:#eee; padding:2px 6px; border-radius:3px; margin-right:4px;"e${tag}c/spane`).join("")}c/spane`
                : '';

              return `
                ctr class="topic-list-item"e
                  ctd class="main-link clearfix"e
                    cdiv style="display: flex; align-items: center; gap: 16px; padding: 8px 0;"e
                      cdiv style="flex-shrink: 0;"e
                        ca class="avatar-link" href="/u/${post.username}"e
                          cimg loading="lazy" width="45" height="45" src="${avatarUrl}" class="avatar" alt="${post.username}"e
                        c/ae
                      c/dive
                      cdiv style="display: flex; flex-direction: column; justify-content: center; padding-top: 8px; padding-bottom: 8px;"e
                        cspan class="link-top-line" style="margin-bottom: 6px;"e
                          ca href="${url}" class="title raw-link"e${excerpt}c/ae
                        c/spane
                        cdiv class="link-bottom-line"e
                          ${categoryHtml}
                          ${tagsHtml}
                        c/dive
                      c/dive
                    c/dive
                  c/tde
                c/tre
              `;
            }).join("");

            // Crea el contenedor de la sección de últimos comentarios
            const latestRepliesContainer = document.createElement("div");
            latestRepliesContainer.className = "latest-replies-container";
            latestRepliesContainer.style.marginTop = "2em";

            latestRepliesContainer.innerHTML = `
              ctable class="topic-list latest-topic-list"
                cthead
                  ctr
                    cth class="default"Últimos Comentariosc/the
                  /ctre
                /theade
                ctbodye
                  ${rows}
                /tbodye
              /tablee
            `;

            container.parentNode.insertBefore(latestRepliesContainer, container.nextSibling);
            container.dataset.modified = "true";
          });
        })
        .catch(error =e {
          console.error("Error al buscar los últimos comentarios:", error);
        });
    }
  });
/script```

אך לוקח בממוצע 2 שניות שהתוכן יופיע. אותו דבר קורה עם הבלוקים בסרגל הצד הימני עם "תשובות אחרונות". האם זה נורמלי?

Why are you adding any code?

This works out of the box without anything special; is it broken for you?

That’s his issue.

I would say it’s expected because the code makes several API requests.
It retrieves the latest posts and then makes one request per topic ID (15 here) to retrieve the category name.

At the moment, I don’t know if there is another way besides using a plugin and making a custom SQL query, for example.

Thats exact.
But the Right Sidebar Blocks make requests too? It gives me the same results for recent replies. Its takes 2 seconds to appear.

I will see how to make a plugin. Thank you

כן, זה עושה את אותו הדבר בהשליפה של הפוסטים האחרונים, אבל זהו זה. זה לא מנסה לקבל את שם הקטגוריה, זו ההבדל.

אני מבין. תודה על העזרה.

אס尝 להציע פתרון אחר

Finally, I am using this for now:

cscript type="text/discourse-plugin" version="0.11.3"
  api.onPageChange(() => {
    if (window.location.pathname === "/") {
      const container = document.querySelector(".latest-topic-list");
      if (!container) return;
      
      // Prevent multiple initializations
      if (window.latestRepliesInitialized) return;
      window.latestRepliesInitialized = true;
      
      // Settings
      const POLLING_INTERVAL = 2000; // 2 seconds
      const COMMENTS_TO_SHOW = 15;
      const CACHE_DURATION = 30 * 60 * 1000; // 30 minutes
      
      // Cache keys
      const CACHE_KEY = "discourse_latest_replies_data";
      const CACHE_TIMESTAMP_KEY = "discourse_latest_replies_timestamp";
      const CACHE_LAST_ID_KEY = "discourse_latest_replies_last_id";
      
      // Store the last seen post ID for comparison
      let lastSeenPostId = parseInt(localStorage.getItem(CACHE_LAST_ID_KEY) || "0");
      let pollingIntervalId = null;
      
      console.log(`Initializing latest comments plugin (last ID in cache: ${lastSeenPostId})`);
      
      // Function to load the latest replies
      function loadLatestReplies(silent = false, forceRefresh = false) {
        // Check cache first, if not a forced update
        if (!forceRefresh) {
          const cachedData = localStorage.getItem(CACHE_KEY);
          const cacheTimestamp = localStorage.getItem(CACHE_TIMESTAMP_KEY);
          const now = Date.now();
          
          // If we have valid cache data
          if (cachedData && cacheTimestamp && now - parseInt(cacheTimestamp) < CACHE_DURATION) {
            try {
              const results = JSON.parse(cachedData);
              console.log(`Using cached data (${results.length} comments, cache from ${Math.round((now - parseInt(cacheTimestamp)) / 1000 / 60)} minutes ago)`);
              renderLatestReplies(results, false);
              
              // If not silent, no need to do more
              if (!silent) {
                return;
              }
              
              // If silent, continue to check for updates
            } catch (e) {
              console.error("Error processing cache:", e);
              // If cache error, continue to fetch fresh data
            }
          } else if (cachedData) {
            console.log("Cache expired, fetching fresh data");
          } else {
            console.log("No cache found, fetching fresh data");
          }
        } else {
          console.log("Forcing update, ignoring cache");
        }
        
        // If it is not silent, show the loading indicator
        if (!silent) {
          // If a comments container already exists, do not show the indicator
          const existingContainer = document.querySelector(".latest-replies-container");
          if (!existingContainer) {
            let loadingIndicator = document.getElementById("latest-replies-loading");
            if (!loadingIndicator) {
              loadingIndicator = document.createElement("div");
              loadingIndicator.id = "latest-replies-loading";
              loadingIndicator.innerHTML = `
                <div style="text-align: center; padding: 20px;">
                  <span class="spinner small"></span>
                  <span style="margin-left: 10px;">Loading recent comments...</span>
                </div>
              `;
              container.parentNode.insertBefore(loadingIndicator, container.nextSibling);
            }
          }
        }
        
        // Fetch the latest data
        fetch("/posts.json?order=created")
          .then(res => res.json())
          .then(data => {
            // Diagnostic log
            if (!silent) {
              console.log("Data received from API:", data.latest_posts.length);
            }
            
            const replies = data.latest_posts
              .filter(p => p.post_number > 1 && !p.topic_slug.includes("private-message"))
              .slice(0, COMMENTS_TO_SHOW);
            
            // Check for new posts
            const maxId = replies.length > 0 ? Math.max(...replies.map(post => post.id)) : 0;
            const hasNewPosts = maxId > lastSeenPostId;
            
            // Diagnostic log
            if (hasNewPosts && !silent) {
              console.log(`New posts detected. Last ID: ${lastSeenPostId}, New max ID: ${maxId}`);
            }
            
            // Update the last seen ID
            if (maxId > lastSeenPostId) {
              lastSeenPostId = maxId;
              localStorage.setItem(CACHE_LAST_ID_KEY, lastSeenPostId.toString());
            }
            
            // If there are no new posts and the check is silent, do nothing
            if (!hasNewPosts && silent && !forceRefresh) {
              return;
     ...