Skip to content

Instantly share code, notes, and snippets.

@paulmolluzzo
Created April 3, 2014 04:28
Show Gist options
  • Save paulmolluzzo/9948278 to your computer and use it in GitHub Desktop.
Save paulmolluzzo/9948278 to your computer and use it in GitHub Desktop.
Improving We Work Meteor RSS Feed

The current implementation of the RSS feed for We Work Meteor (blob used below is here) has an issue when actually used with an RSS reader. Every time a new document is added to a collection, the entire feed is re-published with the whole collection. The reader winds up showing duplicate posts from the last publishing effort.

The solutions I've considered are:

  1. Using amplify to store a the pubDate on the client and then using that to define the earliest post to publish in the feed. Not sure if it's even possible with feed readers.
  2. Add a new property to the collections called published and set it to true once that particular document is published in a feed. Every time the feed is published it would only find published:false documents. An example is below, but the schemas need to be updated too.
  3. Limiting the .find() to just the posts after the last build date. However, that would only show one post.
  4. Limiting the .find() to just posts in the last 24/48/72 hours. I don't know that it solves the issue of post duplication though.
RssFeed.publish('jobs', function(query) {
  var self = this;
  var pubDate = new Date();
  var lastBuildDate = new Date();
  var mostRecent = Jobs.findOne({}, {sort:{createdAt:-1}});
  var secondMostRecent = Jobs.findOne({}, {sort:{createdAt:-1}, skip: 1});
  if(mostRecent)
    pubDate = mostRecent.createdAt;
  if(secondMostRecent)
    lastBuildDate = secondMostRecent.createdAt;

  self.setValue('title', self.cdata('We Work Meteor - Recent Jobs'));
  self.setValue('description', self.cdata('This is a feed of recent jobs posted to We Work Meteor.'));
  self.setValue('link', Meteor.absoluteUrl());
  self.setValue('lastBuildDate', lastBuildDate);
  self.setValue('pubDate', pubDate);
  self.setValue('ttl', 1);

  Jobs.find({published:false}, {sort:{createdAt:-1}}).forEach(function(job) {
    self.addItem({
      title: job.title,
      description: job.description,
      link: Meteor.absoluteUrl('jobs/'+job._id),
      guid: Meteor.absoluteUrl('jobs/'+job._id),
      pubDate: job.createdAt
    });
    ```javascript
    job.update({_id:job._id}, {$set:{published:true}});
  });
});

RssFeed.publish('experts', function(query) {
  var self = this;
  var pubDate = new Date();
  var lastBuildDate = new Date();
  var mostRecent = Experts.findOne({}, {sort:{createdAt:-1}});
  var secondMostRecent = Experts.findOne({}, {sort:{createdAt:-1}, skip: 1});
  if(mostRecent)
    pubDate = mostRecent.createdAt;
  if(secondMostRecent)
    lastBuildDate = secondMostRecent.createdAt;

  self.setValue('title', self.cdata('We Work Meteor - Recent Experts'));
  self.setValue('description', self.cdata('This is a feed of recent experts listed on We Work Meteor.'));
  self.setValue('link', Meteor.absoluteUrl());
  self.setValue('lastBuildDate', lastBuildDate);
  self.setValue('pubDate', pubDate);
  self.setValue('ttl', 1);

  Experts.find({published:false}, {sort:{createdAt:-1}}).forEach(function(expert) {
    self.addItem({
      title: expert.title,
      description: expert.description,
      link: Meteor.absoluteUrl('experts/'+expert._id),
      guid: Meteor.absoluteUrl('experts/'+expert._id),
      pubDate: expert.createdAt
    });
    expert.update({_id:expert._id}, {$set:{published:true}});
  });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment