views:

51

answers:

1

Hi there, I am trying to figure out the best way to generate an XML sitemap (as described here: http://www.sitemaps.org/) for a Grails application. I am not aware of any existing plugins that do this so I might build one. However, I wanted to get the community's input first. Aside from supporting standard controllers/actions, I am thinking it would be nice to support content driven apps as well where the URL might be generated based on a title property for example.

How would you guys go about this? What would you consider and how would you implement it?

Thanks!

+1  A: 

Sitemaps are pretty specific to each app so I'm not sure if there is enough common code to pull out to a plugin.

Here is how we generate our sitemap for http://www.shareyourlove.com. As you can see it's pretty minimal and DRY due to Groovy/Grails's nice XML syntax

class SitemapController extends BaseQueueController {

        def sitemap = {
            def featured = queueFeatured(1)
            render(contentType: 'text/xml', encoding: 'UTF-8') {
                mkp.yieldUnescaped '<?xml version="1.0" encoding="UTF-8"?>'
                urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
                        'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance",
                        'xsi:schemaLocation': "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd") {
                    url {
                        loc(g.createLink(absolute: true, controller: 'home', action: 'view'))
                        changefreq('hourly')
                        priority(1.0)
                    }
                    //more static pages here
                    ...
                    //add some dynamic entries
                    SomeDomain.list().each {domain->
                    url {
                        loc(g.createLink(absolute: true, controller: 'some', action: 'view', id: domain.id))
                        changefreq('hourly')
                        priority(0.8)
                    }
                }
           }
    }
leebutts
Ah, so you're building that on the fly?
UltraVi01
Yeah, due to the dynamic sections and the fact that it doesn't get hit a lot so we didn't bother implementing any caching (but that wouldn't be hard to do).
leebutts