In background.js
, my browser extension periodically fetches a JSON file published in a GitHub public repository.
Now I want it to be nicer to GitHub and its bandwidth limits, and only fetch file again if it was modified since last update:
url='https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json';
fetch(url, {
headers: {'If-Modified-Since': 'Wed, 15 Aug 2022 18:30:00 GMT'}})
.then(response => response.json())
.then(json => console.log('Loaded: ', json));
However I get CORS error I don't understand how to deal with:
Access to fetch at
'https://raw.githubusercontent.com/LearnWebCode/json-example/master/pets-data.json'
from origin 'chrome-extension://...'
has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
It does not have HTTP ok status.
Adding either combination of other headers doesn't make difference:
'Content-Type': 'text/plain;charset=UTF-8'
'Accept': 'application/vnd.github.v3.raw'
'User-Agent': 'my_github_username/my_github_repository (Chrome 104.0.5112.79)'
Neither does using GitHub Pages as an alternative way to access the file.
Update: I easily eliminated CORS error adding raw.githubusercontent.com
to host_permissions
in manifest.json
(thanks @wOxxOm
). However now If-Modified-Since
doesn't seem to be affect server behavior in the way I expect, see followup question with details.