0

I need to fetch JSON file published in a GitHub public repository, but only if it was modified since last time I checked.

How can I make GitHubUserContent to take If-Modified-Since into account?


Unlike my earlier question, now GitHub does allow If-Modified-Since in header, but the attribute doesn't seem to affect the result. Conversely, my background service worker:

  • gets a cached copy if no IMS is specified: Status Code: 200 (from disk cache)
  • gets a fully reloaded content if IMS is specified to very early / extremely recent date, Status Code: 200

And I never experienced 304 Not Modified, no matter what I tried.


When I curl the same resource with If-Modified-Since, I also see that there is no Last-Modified field in response. I wonder if these two facts are interconnected.

curl -I \
  --header 'If-Modified-Since: Tue, 16 Aug 2022 18:11:00 GMT' \
  https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json

Here is my service worker code to test IMS behaviour:

function fetchVaryingIfModSince()
{
  let url = 'https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json'
  // Last committed to GitHub: Mon, 28 Nov 2016 17:36 GMT
  
  var timestamp1 = 'Sun, 27 Nov 2010 12:00:00 GMT';
  var timestamp2 = 'Tue, 16 Aug 2022 18:11:00 GMT';

  fetch(url)  
  .then(response => response.json())
  .then(json => console.log('No IMS: ', json));


  // With timestamp1, should be definitely downloaded:
  // file CHANGED since 2010
  fetch(url, {
    headers: {
    'If-Modified-Since': timestamp1}})
  .then(response => response.json())
  .then(json => console.log('IMS before-commit: ', json));

  // With timestamp2, definitely should NOT be downloaded:
  // file NOT changed since 2022
  fetch(url, {
    headers: {
    'If-Modified-Since': timestamp2}})
  .then(response => response.json())
  .then(json => console.log('IMS after-commit: ', json));
}
jub0bs
  • 60,866
  • 25
  • 183
  • 186
wass rubleff
  • 326
  • 1
  • 14

1 Answers1

4

That service doesn't support If-Modified-Since. However, the response does include an ETag header, and you can use that with If-None-Match.

If-Modified-Since is not a good match for Git repositories because Git doesn't require that timestamps are monotonically increasing and while in the real world UTC timestamps do monotonically increase, unfortunately people can have broken computers where time goes backwards. Thus, If-Modified-Since requires a history walk of the entire branch, whereas If-None-Match does not.

bk2204
  • 64,793
  • 6
  • 84
  • 100