Tuesday, September 2, 2008

Sitemap

It's nice to have a sitemap that adheres to the Sitemap 0.90 protocol. In rails, this can be automatically generated. Unfortunately, you have to do some work.

I found 2 different ways:

1. using controllers
2. using rake

If your site is small, (1) works really well. It's as dynamic as it can be as the sitemap is generated on the fly. However, every time anyone visits that URL, you end up hitting the database for a fairly large call, memory-wise. If it's just legitimate people accessing it, they will access the URL when they need it, or on a periodic time frame. However, if you encounter malicious users (which there are many out there), they could easily overload your server.

(2) is not as up-to-date, but from my point of view, that's ok, especially if you update the files daily. The solution posted works well, but since out site takes advantage of BackgrounDrb, I thought I would modify it to work with BackgrounDrb instead of rake.

I created some self functions to do the database queries and handle the tasks in the rake. Also, in sitemap.rb, I added the lines:

include ActionController::UrlWriter
default_url_options[:host] = 'www.twoblindfish.com'

This allows you to use named routes and url_for. Now we don't need to hardcode our URLs! Another plus! The rest is pretty straight forward. A few of the tasks were redundant, so I refactored them into multiple functions for reuse.

The only thing really different would be to place your sitemaps in public/system/. so that the sitemaps don't get removed when you do a cap:deploy. This works because the public/system folder is symlinked to shared/system.

Hope this helps those in need of generating sitemaps!


W

No comments: