I encounter a problem on Google Script.
The use of the script is to get RSS feed URLs to fetch from a Google Sheet and if new article(s) are detected (comparing the GUIDs from the fetch to a GUID array stored locally), post them in a Google Chat space.
The problem faced is when I fetch the RSS feed URL. The result is the version of the day before, until it fetches the new version once after 45 to 60 minutes after the publication. Most of the time it comes back to the previous version and alternate between the outdated and good versions.
I precise that I use a time based trigger.
I am sure about the delay with the new version since I check it manually several times a day the RSS feed to check the script health.
Logs :
11 Jun 2024, 15:37:05 => lastBuildDate : Mon, 10 Jun 2024 13:06:32 +0000
11 Jun 2024, 15:39:21 => lastBuildDate : Tue, 11 Jun 2024 12:46:24 +0000
11 Jun 2024, 15:49:28 => lastBuildDate : Mon, 10 Jun 2024 13:06:32 +0000
Here is the code used in a function called for all lines in the Google Sheets detected :
var xml,document = null;
var fetchOptions = {
"async": true,
"method": "GET",
"contentType": "application/xml",
"muteHttpExceptions": true,
"followRedirects": true,
headers : {
'Cache-Control' : 'max-age=0',
'Clear-Site-Data' : '*',
"Accept": "*/*",
"Accept-Encoging" : "gzip, deflate, br"
}
}
try{
var now = new Date().getTime()**2;
var url = FEED_URL + "?t=" + now;
Logger.log("Fetching this url : " + url);
//Add the now timestamp to the URL to avoid cached versions
xml = UrlFetchApp.fetch(url,fetchOptions).getContentText();
document = XmlService.parse(xml);
//Log the last build date and the latest item found
Logger.log("lastBuildDate : " + document.getRootElement().getChild("channel").getChild("lastBuildDate").getText());
};
As you can see in the script shown above, I have tried to :
- Assign xml and document variables to null to “reset” these.
- Add ‘Cache-Control’ : ‘max-age=0’ or ‘max-age=1’ or ‘no-cache’ or ‘no-cache’ but non of those options had an impact.
- Add the timestamp on power 2 as a parameter to the end of the URL to try to fetch a “new” URL every time (in fact the same result)
None of these options have worked, I get old versions for 45 minutes every time still.
Is there a way to clear or avoid the cache from the UrlFetchApp from Google Script ?
Thanks by advance for your time and help !
Nonouille is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.