XML sitemap in Grails
Asked Answered
M

2

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!

Magistral answered 20/9, 2010 at 1:3 Comment(0)
B
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'
        }
    }
}
Built answered 20/9, 2010 at 2:30 Comment(5)
Ah, so you're building that on the fly?Magistral
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).Built
@Don I've added them to the answerBuilt
From where did this BaseQueueController come from?Tournai
@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 exampleBuilt
H
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>
Horology answered 16/10, 2018 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.