Eleventy - Fetch data from the Github REST API to populate a projects page
We will create a projects page that is populated with data fetched from the GitHub REST API. The page has a list of project cards like the screenshot below.
We will use the @11ty/eleventy-fetch
plugin to fetch and cache the data. We will refresh this data every 2 days. You can choose whatever interval you like!
What we want is to provide a list of repositories to feature on the page. We will store the list in a JSON data file in the following format:
The GitHub Rest API - which endpoint do we use?
Ideally, we would be able to make a single request with our list of repos and get back the same list of repos but with more detailed information. Unfortunately, the GitHub REST API does not quite offer this, we have 2 options:
- Use the “list repositories for a user” endpoint which will return a list of all public repositories for a user. It is up to us to search through the response to get the details for each of our repos.
- Use the “Get a repository” endpoint endpoint to request the detailed information for a single repo. We would need to make a request for each repo.
I will go with option 1. It should be quicker to make a single request and do more processing, rather than make multiple requests and do less processing.
To make a GET request for public data, it is not necessary to be an authenticated user. The following curl command will return all of the public repos for the user robole
:
This returns up to 100 repos sorted by full_name
in ascending order. Notice I added the per_page
query parameter to increase the number of repos returned from the default of 30 to 100.
Here is a excerpt of the response with only the key fields included:
If you have more than 100 public repos, then you will need to use pagination to make additional requests. I will not cover the pagination use case in this tutorial.
The rate limit for unauthenticated users is 60 per hour. This is more than adequate for our needs.
The code
This is an outline of the project files focusing only on the files we will cover:
Let’s go through the files to see how we get the remote data and use it a webpage.
Our project list - projects.json
This is the data file that has an array of the projects we want to feature on our website. This data will be available in our page templates as a projects
variable.
Fetching the data - github.js
This is the data file that will fetch the public repos for an user. This data will be available in our page templates as a github
variable.
I use the @11ty/eleventy-fetch
plugin to fetch the data from the API and cache the results. The plugin enables you to request the data at a configurable interval to save on network requests on subsequent builds. I chose to fetch the data every 2 days. By default, the data is stored in the .cache folder.
You can provide parameters to the fetch request through the fetchOptions
object, the options are the same as node-fetch
, which is used under the hood. We provide the required 2 headers for making a request to the “list repositories for a user” GitHub endpoint.
You should be careful that you do not store sensitive data in the cache folder and check it into the git repository. We are just requesting public data, so we can do it without any risk. Having the cache folder allows us to reuse the data for each build.
If you do not want to check the cache folder into your git repository, some hosts let you persist a cache folder between builds. For example, Netlify has the netlify-plugin-cache
package for this purpose.
Create a find_repo
filter to find the repo by its URL - eleventy.config.js
We will create a filter to find a GitHub repo by its URL in our remote data. This will make our projects.njk template cleaner. It is the html_url
field in the response that we will inspect.
Doing this in JavaScript is more efficient because we can use the find()
array function to return the first match and look no further. In Nunjucks, we are limited to using a for
loop and would have to loop through the entire array of repos since there is no break
statement available in Nunjucks.
Our projects page - projects.njk
Our projects.njk template will become our /projects/index.html
page. We have access to the projects
variable to list our featured projects. We want to loop through this array and show each project as a project card. The project card is wrapped in a link pointing to the GitHub repository.
We use the find_repo
filter to get the remote repo instance for each project contained in our github
variable. We use the fields: description
, language
, and stargazers_count
to populate our card.
Source code
You will find the code in the github-projects
subfolder of the https://github.com/robole/eleventy-tutorials repo.
Did you enjoy this tutorial? If you did, could you give the repo a star to let me know please? 🌟🫶
Conclusion
Fetching remote data and using it to build prerendered pages is powerful. I like this particular use case because it ensures that I do not forget to update the description for my repos on GitHub. I do not need to duplicate data on my own website when I want to share information on some of my projects.
Using the eleventy plugin @11ty/eleventy-fetch
, we can fetch and use remote data with minimal code. It is done efficiently by caching results and only makes requests when we want to refresh the data. It has such a minor impact on build times, you feel like you are using local data most of the time.
If you want to see a variation of this in the wild, you can visit my projects page!