7

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!

2 Answers 2

16

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'
        }
    }
}
5
  • Ah, so you're building that on the fly?
    – RyanLynch
    Sep 20, 2010 at 2:47
  • 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
    Sep 20, 2010 at 3:29
  • @Don I've added them to the answer
    – leebutts
    Feb 10, 2012 at 8:08
  • From where did this BaseQueueController come from? Aug 3, 2012 at 18:57
  • 1
    @CassioLandim it was our own base class with logic common to our application. It does not contain anything specific to the sitemap generation, I'll update the example
    – leebutts
    Aug 6, 2012 at 3:55
0

I was doing a sitemap on Grails with a UrlMappings.groovy and does not need a controller for this practice. I putted the next code in the UrlMappings:

"/robots.txt" (view: "/robots")
"/sitemap.xml" (view: "/sitemap")

And I create my sitemap as gsp with xml encoding, example of sitemap.gsp:

<?xml version="1.0" encoding="UTF-8"?>

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

   <url>

      <loc>http://www.putYourSite.com.py/</loc>

      <lastmod>putAdate</lastmod>

      <changefreq>daily</changefreq>

      <priority>1.0</priority>

   </url>

</urlset>

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.