Kyle Banks

Creating a Simple RSS Feed for Your Grails Site

Written by @kylewbanks on Mar 16, 2013.

With all of the news about Google shutting down Reader on July 1st, there seems to be an uprising of new RSS feed readers cropping up to try and fill the void.

All of this got me thinking that I should make a quick RSS feed for my site, and came up with the following solution. It's a very simple solution, and should take no longer than a few minutes to set up. The most important parts are to ensure the pubDate format is correct, and to set the response content-type to application/xml.

In your Controller:

def rss = {
    DateFormat pubDateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
    def postList = Post.list(max: 15)

    [posts: postList, formatter: pubDateFormatter]

And the corresponding view (rss.gsp):

<rss version="2.0">
        <title>Kyle W. Banks</title>
        <description>A collection of tips, tricks, and tutorials covering a wide range of programming topics.</description>
            <title>Kyle W. Banks</title>
        <g:each in="${posts}" var="post">
              <description><![CDATA[ ${post.body} ]]></description>

Finally, add the following link to your site's head to help your RSS feed be discovered by readers, and you're set:

<link rel="alternate" type="application/rss+xml" title="Kyle W. Banks RSS Feed" href="" />

For an example on how this should look, click here.

I plan to add some tracking to this, and perhaps see if I can somehow get the events into Google Analytics. If I can get Google Analytics to track the server side events, I'll be sure to post back with an update on how I did it.

Let me know if this post was helpful on Twitter @kylewbanks or down below, and follow me to keep up with future posts!