9

I am using the Google Feed JSAPI to read/parse a feed. The problem is, when the feed changes, the previous entries become invalid (links and images don't work) so I cannot load a cached version of the feed. I thought there would be an option when loading the feed to not use the cached version but I don't see one. My solution is to do add in a variable (t) to the end of the feed url so it is "unique" but this seems hacky (but it works). Anyone know of a better way to do it?

    function onLoad() {
      // Create a feed instance that will grab feed feed.
      var feed = new google.feeds.Feed(feedLocation+"&t="+new Date().getTime());

      // Request the results in XML (so that we can parse out all the info
      feed.setResultFormat(google.feeds.Feed.XML_FORMAT);

      //must set this - if you don't, it defaults to 4
      feed.setNumEntries(50);

      // Calling load sends the request off.  It requires a callback function.
      feed.load(feedLoaded);
    }
Brian Pipa
  • 808
  • 9
  • 23
  • Did you find a solution? The problem here is that the server providing the feed is using caching based on the feed's URL. So if I add a random string to that URL's query string, then this will not only bypass Google's caching but also that of the server. Guess I have to modify the server to add a special *bypass external cache* parameter which gets ignored when doing internal caching. – feklee Feb 19 '13 at 12:08
  • Nope, no solution other than what I mentioned in my question - adding in the time as a "random" parameter. It works, just not as clean as I'd like. – Brian Pipa Feb 20 '13 at 17:39
  • 6
    I'm doing the same now, too, adding something like: `'?bypass_cache=' + Math.floor(Date.now() / refreshIntervalInMs)` – feklee Feb 20 '13 at 18:47
  • 1
    Ah - that's not a bad solution - at least it does cache it but it does so in a way that is controlled by refershIntervalInMs. Still a hack, but more elegant than mine which NEVER uses the cache. – Brian Pipa Apr 12 '13 at 20:56

2 Answers2

2

This answer might be coming in late, but I came across this post and got the solution using pxpgraphics comment. For those who need to invalidate the cache, this will do it. Note that this code example is pulling other data from the RSS feed; modify for your needs.

google.load("feeds", "1");

function initialize() {
    var feedLocation = "http://feeds.bbci.co.uk/news/rss.xml";

    /*  Use the randomNum and the time to invalidate the Google cache and 
        fetch the latest articles.
    */
    var randomNum = Math.floor((Math.random() * 10000) + 1);
    var url = feedLocation + "?&t=" + new Date().getTime() + randomNum;

    var feed = new google.feeds.Feed(url);
    feed.setNumEntries(1000);
    feed.load(function(result) {
    if (!result.error) {
      var container = document.getElementById("feed");
      for (var i = 0; i < result.feed.entries.length; i++) {
        var entry = result.feed.entries[i];
        var div = document.createElement("div");
        div.appendChild(document.createTextNode(entry.title));
        div.innerHTML += "<br>" + entry.publishedDate;
        div.innerHTML += "<br>" + entry.content;
        container.appendChild(div);
      }
    }
  });
}
google.setOnLoadCallback(initialize);

This code assumes you have this DIV on the page:

<div id="feed"></div>
Alex
  • 34,699
  • 13
  • 75
  • 158
  • That works, but it's no better than what I was doing with "new Date().getTime()". I was hoping for something more elegant that didn't require tacking on a "random" parameter and actually did use the cache. So far, the solution @feklee proposed using refreshIntervalInMs is closest to what I wanted. – Brian Pipa May 20 '15 at 17:11
  • You're right. I wish there was a built-in way to tell Google to not cache it. Maybe they prefer this way, as it probably requires less work for their servers. – Alex May 20 '15 at 17:57
0

try feed.includeHistoricalEntries(); It might solve your issue.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • (BTW, keep the Vogon poetry going!) – Daniel Monteiro Jan 30 '13 at 22:40
  • From the API docs, that doesn't sound like it will do what I need. In fact, it sounds like I want dontIncludeHistoricalEntries(). I'll give it a try though and see. RE: Vogon Poetry - I will! :) http://myvogonpoetry.com is my blog :) – Brian Pipa Feb 01 '13 at 15:32
  • 1
    Oh ok, sorry. So, you want to "invalidate" the cache. Maybe asking for the URL with a random parameter? – Daniel Monteiro Feb 05 '13 at 00:27
  • 1
    That's exactly what I said I was doing now: "My solution is to do add in a variable to the end of the feed url so it is "unique" but this seems hacky (but it works)" but I was looking for something that was less of a hack :) – Brian Pipa Apr 12 '13 at 20:54
  • 1
    This random parameter can also be a random number.. `var randomNum = Math.floor((Math.random() * 10000) + 1);` `var url = feedLocation+"&t="+new Date().getTime() + randomNum;` – pxpgraphics Dec 15 '14 at 23:00
  • Thanks, @pxpgraphics. The random number solution worked for me. – Alex May 13 '15 at 19:15