🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
CMS & Integrations••By Komal Nakrani

Astro Giscus Comments: Add GitHub Discussions to a Blog

Astro Giscus setup: enable GitHub Discussions, build a reusable Comments component, lazy-load it, sync light and dark themes, and keep it working with ClientRouter.

#Astro#Giscus#Comments#GitHub Discussions#Blogging

To add Giscus comments to an Astro blog, make a public GitHub repository with Discussions turned on, install the giscus GitHub app on it, copy the repository and category IDs from giscus.app, and render the giscus <script> tag in a Comments.astro component at the bottom of your post layout. Comments are stored as GitHub Discussions, so the Astro Giscus setup adds no database, no server and no adapter to a static site.

This guide builds a reusable component, explains each option, and fixes the two things that usually break: dark mode and page navigation with <ClientRouter />.

Key Takeaways

  • Giscus needs a public repo, Discussions enabled, and the giscus app installed on that repo.
  • Use data-mapping="pathname" so each post URL maps to one discussion.
  • Pass the config as props and render the script with is:inline so Astro doesn’t bundle it.
  • Change the theme at runtime by posting a setConfig message to the giscus iframe.
  • With <ClientRouter />, create the script on astro:page-load so comments load after navigation.

What Giscus is and when it fits

Giscus is a free, open-source comment widget. It loads an iframe from giscus.app that reads and writes GitHub Discussions through the giscus GitHub app. Readers sign in with GitHub to comment or react, and every thread is a normal discussion you can moderate, lock or answer from GitHub.

That trade-off decides whether it suits your blog:

  • Good fit: developer blogs, docs sites, open-source project sites. Your readers already have GitHub accounts.
  • Poor fit: blogs for a general audience. Requiring a GitHub login will stop most readers from commenting.

If your readers aren’t developers, compare the alternatives in the Astro comment systems guide before committing.

Step 1: prepare the GitHub repository

  1. Pick or create a public repository. It can be your site’s repo or a dedicated one like my-blog-comments. Private repos won’t work, because visitors must be able to read the discussions.
  2. In the repo, go to Settings > General > Features and tick Discussions.
  3. Install the giscus GitHub app and grant it access to that repository only.
  4. In the Discussions tab, create a category for comments. An Announcements-type category is recommended, because only maintainers and giscus can create new discussions in it, which stops people from starting random threads.

Now open giscus.app, type owner/repo, choose your mapping and category, and the page generates a script tag with your data-repo-id and data-category-id. Those two IDs are what you need.

Step 2: build a Comments component

Rather than pasting the generated snippet into a layout, wrap it in a component with typed props. This keeps the IDs in one place and makes per-page overrides easy:

---
// src/components/Comments.astro
interface Props {
  term?: string;
}

const { term } = Astro.props;

const config = {
  repo: 'your-name/your-blog-comments',
  repoId: 'R_kgDOxxxxxxx',
  category: 'Comments',
  categoryId: 'DIC_kwDOxxxxxxxx',
};
---
<section class="comments" aria-labelledby="comments-title">
  <h2 id="comments-title">Comments</h2>
  <script
    is:inline
    src="https://giscus.app/client.js"
    data-repo={config.repo}
    data-repo-id={config.repoId}
    data-category={config.category}
    data-category-id={config.categoryId}
    data-mapping={term ? 'specific' : 'pathname'}
    data-term={term}
    data-strict="1"
    data-reactions-enabled="1"
    data-emit-metadata="0"
    data-input-position="top"
    data-theme="preferred_color_scheme"
    data-lang="en"
    data-loading="lazy"
    crossorigin="anonymous"
    async
  ></script>
</section>

is:inline matters here. Without it, Astro treats the <script> as something to process and bundle. With it, the tag is written to the HTML exactly as is, which is what a third-party loader expects. The script tag guide explains the difference.

Then add it to your post layout, after the article body:

---
// src/layouts/PostLayout.astro
import Comments from '../components/Comments.astro';
const { frontmatter } = Astro.props;
---
<article>
  <slot />
</article>
{frontmatter.comments !== false && <Comments />}

A comments: false field in a post’s frontmatter now turns comments off for that post. If you use content collections, add comments: z.boolean().default(true) to the schema so the field is typed.

What the Giscus options do

Attribute Recommended value Why
data-mapping pathname One discussion per URL path, independent of title changes
data-strict 1 Matches discussions by a hash of the term, avoiding fuzzy matches between similar paths
data-input-position top Comment box above the thread, easier to find on long threads
data-loading lazy The iframe loads only when it’s near the viewport
data-theme preferred_color_scheme Follows the reader’s OS setting
data-emit-metadata 0 Only needed if you listen for discussion metadata messages

The mapping deserves a second look. With pathname, giscus creates a discussion titled with the path the first time someone comments. If you later change a post’s slug, the old discussion won’t follow it. Either keep slugs stable or pass term explicitly for important posts, for example the entry ID from your collection. Your trailingSlash setting in astro.config.mjs also affects the path, so set it before comments start arriving.

Step 3: match your site’s dark mode

preferred_color_scheme follows the operating system. If your site has its own theme toggle, the iframe won’t know about it. Giscus accepts a setConfig message from the parent page, so you can update the theme without reloading:

---
// src/components/Comments.astro (add below the section)
---
<script is:inline>
  function setGiscusTheme() {
    const theme = document.documentElement.dataset.theme === 'dark' ? 'dark' : 'light';
    const frame = document.querySelector('iframe.giscus-frame');
    frame?.contentWindow?.postMessage(
      { giscus: { setConfig: { theme } } },
      'https://giscus.app',
    );
  }
  new MutationObserver(setGiscusTheme).observe(document.documentElement, {
    attributes: true,
    attributeFilter: ['data-theme'],
  });
  window.addEventListener('message', (e) => {
    if (e.origin === 'https://giscus.app') setGiscusTheme();
  });
</script>

This assumes your toggle sets data-theme="dark" on <html>. Adjust the selector if yours uses a class. The message listener applies the theme once giscus finishes loading, because the iframe sends messages to the parent when it’s ready. For the initial render, set data-theme on the loader script from the same value your toggle stores, so readers don’t see a flash of the wrong theme.

Giscus ships many built-in themes (light, dark, dark_dimmed, transparent_dark and others), and you can pass a URL to your own CSS file for full control.

Step 4: keep comments working with ClientRouter

If your blog uses Astro’s <ClientRouter /> for view transitions, navigation swaps the page content without a full reload. The giscus loader ran on the first page and won’t run again, so a reader who clicks from one post to another sees an empty comments section.

The fix is to create the script element yourself on every page load:

---
// src/components/Comments.astro (ClientRouter version)
---
<section class="comments" aria-labelledby="comments-title">
  <h2 id="comments-title">Comments</h2>
  <div id="giscus-container"></div>
</section>

<script>
  const attrs: Record<string, string> = {
    src: 'https://giscus.app/client.js',
    'data-repo': 'your-name/your-blog-comments',
    'data-repo-id': 'R_kgDOxxxxxxx',
    'data-category': 'Comments',
    'data-category-id': 'DIC_kwDOxxxxxxxx',
    'data-mapping': 'pathname',
    'data-strict': '1',
    'data-reactions-enabled': '1',
    'data-input-position': 'top',
    'data-theme': 'preferred_color_scheme',
    'data-loading': 'lazy',
    crossorigin: 'anonymous',
  };

  document.addEventListener('astro:page-load', () => {
    const container = document.getElementById('giscus-container');
    if (!container || container.childElementCount) return;
    const script = document.createElement('script');
    for (const [k, v] of Object.entries(attrs)) script.setAttribute(k, v);
    script.async = true;
    container.append(script);
  });
</script>

This version uses a bundled <script>, which Astro runs once. The astro:page-load listener it registers fires on the first load and after every navigation, so giscus is injected into each new post. Without <ClientRouter />, the simple is:inline version from Step 2 is all you need.

Performance and privacy

Giscus loads nothing until the reader scrolls near the comments when data-loading="lazy" is set, so it has little effect on how fast the post itself appears. The iframe does load from giscus.app and talks to GitHub, which means readers’ browsers contact third-party servers. Mention giscus and GitHub in your privacy policy, and if you need consent before any third-party request, render a “Load comments” button and inject the script on click instead of on page load.

Moderation happens in GitHub. You can edit, hide or delete comments, lock a discussion, and block users at the repository or organization level. Notifications arrive like any other GitHub discussion activity.

For a blog that’s ready to drop a component like this into, Astro blog themes come with post layouts where comments slot in below the article.

FAQ

Is Giscus free to use with Astro?

Yes. Giscus is free and open source, and GitHub Discussions are free on public repositories. There are no ads, no tracking scripts from giscus itself, and no paid tier.

Do readers need a GitHub account to comment with Giscus?

Yes. Anyone can read comments, but posting or reacting requires signing in with GitHub. That’s why Giscus suits developer audiences better than general ones.

Why do Giscus comments disappear after navigating with view transitions?

With <ClientRouter />, the giscus loader script runs only on the first page. Inject the script inside a document.addEventListener('astro:page-load', ...) callback so it runs after every navigation.

Can I use a private repository for Giscus comments?

No. Visitors must be able to read the discussions, so the repository has to be public. You can use a separate public repo just for comments and keep your site’s source private.

How do I change the Giscus theme when my Astro site switches to dark mode?

Post a message to the giscus iframe with { giscus: { setConfig: { theme: 'dark' } } } and target origin https://giscus.app. Trigger it from your theme toggle or a MutationObserver on the <html> element.