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

Setting up this Blog. #1

Open
preetjdp opened this issue Jun 20, 2020 · 0 comments
Open

Setting up this Blog. #1

preetjdp opened this issue Jun 20, 2020 · 0 comments

Comments

@preetjdp
Copy link
Owner

preetjdp commented Jun 20, 2020


title: 'Setting up this Blog.'
slug: 'setting-up-this-blog'
excerpt: 'Short outlook onto how this blog is setup'
date: '2020-06-20'

Alright, Hey there I'm gonna tell you how to set up this blog powered by Github Issues.
But before that the backstory. Ah heck Skip it

So on the 20th of June, Viranchee shared that he's building his blog entirely powered by Github Issues and linked to this blog as a reference.

The Blog My Reaction
My Reaction

And I really liked the idea, and immediately set out to get this working.

The Process.

Step 0 was to figure out what frontend to use, Gatbsy stood out because it favors GraphQL for data querying and I thought pairing it with the Github API should be seamless.

Arguably one of the best blogs out there is Overreacted By Dan Abramov. So step 1 was to just fork it out and figure it how it's working internally.

Overreacted uses The Remark Transformer Plugin to look for all the Markdown files in the project, process them and make them available to consume under GraphQL.

Well that's useless for us because we want to fetch markdown from Github, not from files on our computer.

Conveniently Gatsby has a Plugin to easily add external GrapqQL Datasources.

It was fairly simple to get data from the Github API. Pipe that with the Gatsby createPage Function, and that's basically the working of the blog.

Code that makes this work.
  // Fetch issues from GraphQL.
  const result = await graphql(
    `
      {
        github {
          viewer {
            repository(name: "Blog") {
              issues(first: 20) {
                nodes {
                  id
                  title
                  bodyHTML
                }
              }
            }
          }
        }
      }
    `
  )

  // Generate Pages for each Post.
  const posts = result.data.github.viewer.repository.issues.nodes
  posts.forEach((post, index) => {
    createPage({
      path: post.number.toString(),
      component: blogPost,
      context: {
        slug: post.number
      },
    })
  })

Steps for Setup.

  1. Set up the project locally.

Clone the project and install the dependencies.

git clone https://github.com/preetjdp/Blog
cd Blog
npm install
# If you get a libpng error while running this
# run `npm rebuild`
  1. Set up a Repo to host your Issues for the blog

Something like https://github.com/YOUR_NAME/Blog

  1. Get a Github Personal Token

A token can be obtained from this URL. Here are the options that I used.

image

  1. Running the Project.

Now that you've obtained your token just use it to run the app.

# If you're running Windows / Powershell
$env:GITHUB_TOKEN="GITHUB_TOKEN"
npm run develop

# If you're using Linux / MacOS
GITHUB_TOKEN="GITHUB_TOKEN" npm run develop

And that's it 🎉🎉. You should have a gatsby project running on http://localhost:8000

Wait How does this work? The Environment Variable is sent as a `Bearer` Auth Header to the Github API.

Authorization: `Bearer ${process.env.GITHUB_TOKEN}`

  1. Personalizing the Blog

Most of the configuration is done through the gatsby-config.js file.
You can change the fields here's as per your liking.

Blog/gatsby-config.js

Lines 2 to 12 in 530dfbb

siteMetadata: {
title: `Blog`,
author: `Preet Parekh`,
description: `Personal Blog`,
siteUrl: `https://preet-blog.netlify.app/`,
social: {
twitter: `TmPreet`,
mail: `[email protected]`
},
repoName: `Blog`
},

Quirks of this Approach.

The data is not loaded at runtime.

Wait Whaaaat?? What does this mean?
When you change the content of an issue (In the pursuit of editing a blog post). It won't work unless you build it afterward and publish it.

This is a blessing in disguise, this allows the blog to be ridiculously fast.
Lighthouse Test Result

References

@preetjdp preetjdp changed the title Setting up the Blog. Set up the Blog Jun 20, 2020
@preetjdp preetjdp changed the title Set up the Blog Setting up the Blog Jun 20, 2020
@preetjdp preetjdp changed the title Setting up the Blog Setting up this Blog Jun 21, 2020
@preetjdp preetjdp changed the title Setting up this Blog Setting up this exact Blog Jun 21, 2020
@preetjdp preetjdp changed the title Setting up this exact Blog Setting up this exact Blog. Jun 21, 2020
@preetjdp preetjdp changed the title Setting up this exact Blog. Setting up this Blog. Jun 21, 2020
@preetjdp preetjdp added the Blog This represents a Blog Post label Jun 22, 2020
@preetjdp preetjdp removed the Blog This represents a Blog Post label Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant