Dave Silvester
2006-Feb-07 13:27 UTC
[Rails] Page caching when name contains a dot (period)
Hi Everyone, Just thought I''d post some details of an interesting little quirk I''ve found with page caching. For a site I''ve just built for a small music festival, there are a number of artist profiles. I''m storing the artists in my database, but using caches_page to speed things up when displaying the details to the public. I''ve also made it so that instead of the numerical ID appearing in the URL, it uses the artist''s name instead. So if someone called "Bob Smith" was playing, the URL would be: /artists/Bob+Smith Now, with the page caching, it works fine for most of the artists, since Bob Smith''s cached page gets stored in myapp/public/artists/Bob+Smith.html But what if "B. Smith" was playing - you''d expect it to generate "B.+Smith.html", but it doesn''t, just a file called "B.+Smith" with no ".html" on the end of it. It seems the caching system does not add a file extension to a page name that already has a dot (period) in it. The end result of this is that Apache (this is on Dreamhost, btw) fails to recognise that the cached page is HTML (since it has no extension) and serves the raw HTML as text/plain... not pretty! Furthermore, you won''t notice this on the first page view, since at the point it generates the cached page using Rails, I guess it''s serving up the result from the Rails controller, rather than the newly generated cached page. This problem only becomes apparent on the *second* page view - and only then if you do a shift-reload forced refresh from the server to bypass your local browser''s cache. I quickly tried renaming the problematic cache files and putting ".html" on the end of their names just to see if that helped, but then we''re back to square one - the cache thinks it''s the first time that page has been viewed, so a new file without a ".html" gets created (and works first time), and then you''re back to the raw HTML being served up as plain text on subsequent views. So, the workaround for this is to (assuming all the pages that are being cached should be served as text/html) make a .htaccess file with the following line in it, and put it in the same directory as the problematic cached files: ForceType text/html That solves the problem and serves all the pages (even those without file extensions) as text/html - though whether this is a bigger issue (maybe even a slight bug) with caches_page, I guess is up for debate. Anyway, I thought I''d post my easy workaround in case anyone else finds themselves in the same situation. (Though surely I''m not the first one to encounter this?) Cheers, ~Dave -- Dave Silvester Rent-A-Monkey Website Development http://www.rentamonkey.com/ PGP Key: http://www.rentamonkey.com/pgpkey.asc
Dave Silvester wrote:> So, the workaround for this is to (assuming all the pages that are being > cached should be served as text/html) make a .htaccess file with the > following line in it, and put it in the same directory as the > problematic > cached files: > > ForceType text/html > > That solves the problem and serves all the pages (even those without > file > extensions) as text/html - though whether this is a bigger issue (maybe > even > a slight bug) with caches_page, I guess is up for debate. > > Anyway, I thought I''d post my easy workaround in case anyone else finds > themselves in the same situation. (Though surely I''m not the first one > to > encounter this?)I can''t be sure whether you are the first, but you certainly weren''t the last. Thanks for this. I ended up putting that directive in the overall apache2 config file, instead of using the .htaccess, because then manually removing the cache doesn''t remove the solution. Thanks, ben -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.