7

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:

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127

2 Answers2

0

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.

Brendan Forster
  • 2,548
  • 1
  • 24
  • 32
-1

Github pages DOES NOT have a built in way of accessing and caching the latest release from the API. You seem to have chosen javascript to get the data. Therefore caching the response only affects one user, and that is not what you are after, I assume. What IS possible...

Using dynamic data

You get the values/data in an ajax request, so you can write the result real-time to the required page. You could store the result in localstorage to prevent any additional calls for this user.

Using static data

You can add the JSON response as a static file to the _data folder. You can do this manually and then loop over the data.

If doing things manually is not your cup of tea, you can automate getting the file during your Jekyll build using a plugin.

Unfortunately this plugin only works with public JSON files (no authentication is currently available). You might want to add the authentication yourself, as the author of the plugin suggests on his Github page.

You could also create a PHP page that does the authentication and caching of the data for you, using this PHP Github API. You simply let the PHP generate the wanted JSON data/output on a public URL. In that case the mentioned Jekyll plugin is perfectly usable without any adjustments.

Automating your build

From your research items I can see you also want to automate/schedule the build process. An easy way to do this is by using the scheduled builds feature on the CloudCannon platform.

Mr. Hugo
  • 11,887
  • 3
  • 42
  • 60