Skip to content

Giscus Javascript not loading iframe correctly #12567

@andrewheiss

Description

@andrewheiss

Bug description

At some point recently, the Javascript code for loading Giscus seems to have stopped working correctly. The loadGiscusWhenReady() Javascript function doesn't load the remote iframe, but manually pasting in the contents of loadGiscusWhenReady into a browser's Javascript console and then running loadGiscus() does load the iframe.

Steps to reproduce

This is a tricky bug to create a reprex for, since it requires a full github repo with discussions enabled and Giscus enabled, but here's what I did to recreate the issue.

Steps 1-4 are all available in a little mini testing website at https://andrewheiss.quarto.pub/quarto-giscus-testing/ and in a GitHub repo at https://github.com/andrewheiss/quarto-giscus-testing

  1. At the terminal, run quarto create project blog and create a new blog project with all the default options

  2. Push new blog project to GitHub and enable Giscus on that repository

  3. Add this to _quarto.yml (but with correct options for category and category-id)

    website:
      title: "quarto-giscus-testing"
      comments:
        giscus:
          repo: andrewheiss/quarto-giscus-testing
          category: General
          category-id: DIC_kwDOOa-qAc4CpLmu
          mapping: pathname
  4. Render the site and publish it with quarto publish (I used Quarto Pub: https://andrewheiss.quarto.pub/quarto-giscus-testing/)

  5. Visit a page like https://example.com/posts/post-with-code/ and inspect the page—or go to https://andrewheiss.quarto.pub/quarto-giscus-testing/posts/post-with-code/ for a live sample page. Giscus-related HTML will be included, but no Giscus iframe widget will appear.

Image
  1. In the Javascript console, try running loadGiscusWhenReady();. Nothing will happen.
Image
  1. In the Javascript console, paste and run all this—these are the contents of the loadGiscusWhenReady() Javascript function, but loaded without being inside a function:
 // Function to get the theme based on body class
  const getTheme = () => {
    const baseTheme = document.getElementById('giscus-base-theme').value;
    const altTheme = document.getElementById('giscus-alt-theme').value;
    return document.body.classList.contains('quarto-dark') ? altTheme : baseTheme;
  };
  // Create the Giscus script and add it to the desired location
  const loadGiscus = () => {
    const script = document.createElement("script");
    script.src = "https://giscus.app/client.js";
    script.async = true;
    script.dataset.repo = "andrewheiss/quarto-giscus-testing";
    script.dataset.repoId = "R_kgDOOa-qAQ";
    script.dataset.category = "General";
    script.dataset.categoryId = "DIC_kwDOOa-qAc4CpLmu";
    script.dataset.mapping = "pathname";
    script.dataset.reactionsEnabled = "1";
    script.dataset.emitMetadata = "0";
    script.dataset.inputPosition = "top";
    script.dataset.theme = getTheme();
    script.dataset.lang = "en";
    script.crossOrigin = "anonymous";
    // Append the script to the desired div instead of at the end of the body
    document.getElementById("quarto-content").appendChild(script);
  };
  // MutationObserver to detect when the 'quarto-light' or 'quarto-dark' class is added to the body
  const observer = new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      if (mutation.type === "attributes" && mutation.attributeName === "class") {
        if (document.body.classList.contains('quarto-light') || document.body.classList.contains('quarto-dark')) {
          loadGiscus();
          observer.disconnect(); // Stop observing once Giscus is loaded
          break;
        }
      }
    }
  });
  // Start observing the body for class attribute changes
  observer.observe(document.body, {
    attributes: true,
    attributeFilter: ["class"],
  });
  1. In the Javascript console, run loadGiscus();. The commenting iframe will appear.
Image

Actual behavior

Basically, the loadGiscusWhenReady() Javascript function doesn't seem to work anymore and doesn't do anything.

Giscus can be loaded manually by running all the internal function parts (i.e. const GetTheme = () ..., const loadGiscus = () ..., etc.) and then running loadGiscus() in the console.

Expected behavior

The Giscus widget should display

Your environment

This happens in Chrome, Firefox, and Safari on macOS and Windows and iOS Safari

Quarto check output

Quarto 1.7.23
[✓] Checking environment information...
      Quarto cache location: /Users/andrew/Library/Caches/quarto
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.6.3: OK
      Dart Sass version 1.85.1: OK
      Deno version 1.46.3: OK
      Typst version 0.13.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.7.23
      Path: /Applications/quarto/bin

[✓] Checking tools....................OK
      TinyTeX: (external install)
      Chromium: (not installed)

[✓] Checking LaTeX....................OK
      Using: TinyTex
      Path: /Users/andrew/Library/TinyTeX/bin/universal-darwin
      Version: 2024

[✓] Checking Chrome Headless....................OK
      Using: Chrome found on system
      Path: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
      Source: MacOS known location

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
      Version: 3.13.1
      Path: /opt/homebrew/opt/[email protected]/bin/python3.13
      Jupyter: 5.7.2
      Kernels: python3

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggiscusGiscus comments integrationthemesRelated to HTML theming or any other style related issue (like highlight-style)

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions