Prevent Google Feed API from using cached feed

北慕城南 提交于 2019-11-27 06:47:44

问题


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

回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/13401936/prevent-google-feed-api-from-using-cached-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!