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));
}