Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unleash Your Dev Blog: Write More with GitHub Issues as Your CMS #18

Open
abdulrcs opened this issue Apr 2, 2024 · 4 comments
Open

Comments

@abdulrcs
Copy link
Owner

abdulrcs commented Apr 2, 2024


slug: unleash-your-dev-blog-write-more-with-github-issues-as-your-cms
date: 02-Apr-2024
summary: Turn your GitHub Issues into a powerful Next.js blog to write more and publish faster!
readingTime: 3 min read
image: https://github.com/abdulrcs/abdulrahman.id/assets/54136956/37ce03da-b38f-4e32-84dd-c90636dcc2d6

In this article, I'm going to show you how to leverage Github Issues as a CMS, and integrate it into your Next.js blog 🚀

GIF

I recently migrated my open-source portfolio website from using Contentful API as a CMS (Content Management System) to GitHub Issues instead.

Here's the detailed PR I made in my portfolio repo to migrate Contentful API to use GitHub Issues if you're interested: abdulrcs/abdulrahman.id#17

Why?

I'm inspired by the blog from swyx.io that uses GitHub issues and comments, as mentioned here.

The friction of writing a GitHub issue is less daunting than using a fully-fledged CMS like Contentful because you have to log in to their page, navigate to the content section, host the images yourself, etc.

This will (hopefully) lead to more writings in the future 🤞

How to use the GitHub API

We can use a GitHub API wrapper using renatorib/github-blog to fetch our GitHub Issues.

Note: GitHub API is limited to 5000 requests/hour, so we need to statically render the blogs using getStaticProps and getStaticPaths to make it scalable, more on that later.

First, generate the GitHub token here.

After that, create new labels in your issue named type:post, state:published, and state:draft.

image

Then, we can create a GitHub issue for our blog post!
Don't forget to add a front matter in the issue to create a slug for our post URL like this:

---
slug: your-slug-title
---

image

Integrating github-blog to Next.js

Installing the Library

Install @rena.to/github-blog in your Next.js project.

yarn add @rena.to/github-blog

Fetching Lists of Posts

Here's an example of how to fetch a list of posts statically using getStaticProps:

export async function getStaticProps() {
  const blog = new GithubBlog({
    repo: 'your-github-username/your-repo',
    token: process.env.GITHUB_TOKEN,
  })
  const posts = await blog.getPosts({
    query: {
      author: 'your-github-username',
      type: 'post',
      state: 'published',
    },
    pager: { limit: 10, offset: 0 },
  })

  return {
    props: {
      posts: posts.edges,
    },
  }
}

Fetching a Post using Slug

In the /blog/[slug] page, you can query the exact post by using the slug:

export async function getStaticProps({ params }) {
  const blog = new GithubBlog({
    repo: 'your-github-username/your-repo',
    token: process.env.GITHUB_TOKEN,
  })
  const data = await blog.getPost({
    query: {
      author: 'your-github-username',
      search: params.slug,
    },
  })
  const metadata = data.post
  const source = metadata.body

  return {
    props: {
      metadata,
      source,
    },
    revalidate: 30,
  }
}

Generating Static Paths for our posts

We need to statically generate our post URL links by fetching all the posts, and mapping each unique slug inside getStaticPaths:

export async function getStaticPaths() {
  const blog = new GithubBlog({
    repo: 'your-github-username/your-repo',
    token: process.env.GITHUB_TOKEN,
  })

  const data = await blog.getPosts({
    query: {
      author: 'your-github-username',
      type: 'post',
      state: 'published',
    },
    pager: { limit: 10, offset: 0 },
  })

  return {
    paths: data.edges.map(({ post }) => ({
      params: { slug: post.frontmatter.slug },
    })),
    fallback: false,
  }
}

Adding Comments Feature

We can use utteranc.es, a lightweight comment widget built on GitHub Issues to integrate authed comments in our blog.

image

This is brilliant because in a Developer Blog of course there's a big chance that our audience has a GitHub profile 😆

Since we already integrated the GitHub Issues as our blog, we can add comments to our blog by adding this script to our page:

<script src="https://utteranc.es/client.js"
        repo="[ENTER REPO HERE]"
        issue-term="title"
        theme="github-dark"
        crossorigin="anonymous"
        async>
</script>

That's it!

MioJimCarreyGIF

I hope that integrating GitHub issues as the source of your posts will empower you to write more consistently and publish faster 🥳

@abdulrcs
Copy link
Owner Author

abdulrcs commented Apr 2, 2024

Copy link

swyxio commented Apr 2, 2024

this is the way!

@abdulrcs
Copy link
Owner Author

abdulrcs commented Apr 2, 2024

this is the way!

This just made my day, thanks for commenting @swyxio! your blog is what gets me started on the whole idea.

I love how you mentioned my post and it linked our GitHub issue, reminds me of the concept of an interconnected digital garden that you and @MaggieAppleton talked about, so neat!

image
source: https://maggieappleton.com/garden-history

Copy link

fgdgdfg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants