Search code examples
cachingjekyllgithub-pagesgithub-apirepo

Does Github pages have a built in way of accessing and caching the latest release from the API?


Learning Jekyll and hosting through Github Pages I'm trying to figure out how to access a private repo's latest release and cache the download URLs to a Jekyll page. I know how to access the data through the Github API with an access token using AJAX:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">  

  let USER = "grim"
  let REPO = "foobar"
  let TOKEN = "jsfjksgfjasgdjgsajgasjk"
  $.ajax({
    url: `https://api.github.com/repos/${USER}/${REPO}/releases/latest?access_token=${TOKEN}`,
    jsonp: true,
    method: "GET",
    dataType: "json",
    contentType: 'application/json',
    success: function(res) {
      console.log(res.assets)
    },
    error: function(res) {
      console.log(res)
    }
  })
</script>

In the config.yml I'm setting the USER, REPO and TOKEN. My research I did find the Cache API but it isn't listed. Using Github Pages as the host and coded in Jekyll is there a way to get the latest release and cache the response on a private repo with Jekyll? If I cannot cache the API is there a way to store the release URLs on the Jekyll build so I can code it to the buttons so the buttons act as a download?

Research:


Solution

  • Using Github Pages as the host and coded in Jekyll is there a way to get the latest release and cache the response on a private repo with Jekyll?

    The Jekyll extensions that are only allowed to be used with GitHub Pages are listed here:

    • jekyll-coffeescript
    • jekyll-default-layout
    • jekyll-gist
    • jekyll-github-metadata
    • jekyll-optional-front-matter
    • jekyll-paginate
    • jekyll-readme-index
    • jekyll-titles-from-headings
    • jekyll-relative-links

    None of these relate to caching or client-side features you're looking for, so you'll have to entertain a different solution.

    If I cannot cache the API is there a way to store the release URLs on the Jekyll build so I can code it to the buttons so the buttons act as a download?

    As GitHub Pages doesn't have a caching API that matches your use case, you could fall back to server-side rendering by making these changes to the site:

    • create a data file in the Jekyll site the stores the current release assets (stub the URLs to simplify testing for now)
    • update the site to use this data file and render HTML (rather than using client-side JS)

    You could then create a GitHub Action that runs periodically to perform this workflow:

    • using a token that has access to the private repository, query for the latest release
    • programatically rewrite the file on disk to use the same values
    • check if it has changed using Git
    • commit and push to the default branch, which would triggering a publish of the site

    I don't have any sample around to illustrate this, but there are other resources out there if you wanted to explore this further.