Search code examples
grailsgrails-plugin

XML sitemap in Grails


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!


Solution

  • 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{
    
            def sitemap = {
                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)
                        }
                    }
               }
        }
    

    URL Mappings

    class UrlMappings {
        static mappings = {
    
            "/sitemap"{
                controller = 'sitemap'
                action = 'sitemap'
            }
        }
    }