Saturday, June 30, 2007

Rails Page Caching and MIME Types

Problem:

Regardless of what your template file extension is, after a page is cached using page caching the content will be served as text/html. This is fine in most cases, but what if you are caching rss or ical feeds? Rails offers a 'page_cache_extension' attribute which allows you to set the cached file extension to whatever you like but this is a class attribute and will cause all cached pages to be of this type, pretty much useless.

Solution:

<LocationMatch \/(rss)\/?>
    ForceType text/xml;charset=utf-8
</LocationMatch>
<LocationMatch \/(ical)\/?>
    ForceType text/calendar;charset=utf-8
</LocationMatch>

What it do?

This solution assumes that you are running apache. Add the above to your apache config within the VirtualHost directive. This examines the requested location and forces a MIME type for locations that match the regex. Locations that match '/rss' or '/rss/' will be served as 'text/xml' and locations that match '/ical' or '/ical/' will be served as 'text/calendar'. Done.

No comments: