Aug
20
Rails page caching with an admin interface
Filed Under General | Leave a Comment
Chris and I recently released the new Web 2.0 Show website and we worked very hard to ensure the performance was as quick as we could make it. We used the ySlow plugin for firefox and made most of the tweaks they suggested, we were able to get to a B (86) before we added things like the twitter script.
The biggest performance gain we made was to turn on rails full page caching for the episodes index, the episode show page, and the archives page. The big issue is, we didn’t build a namespaced admin controller for the site, we embedded the admin stuff into the restful resources, like episodes/new, for inline admin stuff. We put in a simple admin interface (just a bar across the top) but it broke as soon as we turned on page caching. Chris and his boss had just solved this very issue at their work, so I took the tip from Rich and started hacking up a quick javascript method to do an admin navbar.
First, I had to add the route and the controller action to handle the ajax requests for an admin navbar.
routes.rb
map.resources :episodes, :has_many => [ :comments ], :collection => {:archive => :get, :search => :get, :admin_bar => :get}
episodes_controller.rb
def admin_bar
respond_to do |format|
if logged_in?
format.js
else
format.html {render :nothing => true}
end
end
end
Once that was done, I just needed to add the ajax request and the admin_bar div to the application.rhtml, it loads the ajax request each time the page does. I placed the code at the bottom so the admin bar loads last and doesn’t hold up any of the page rendering. I used url_for to make the ajax call, but this blog doesn’t seem to like my code syntax.
new Ajax.Request('/episodes/admin_bar', { asychronous:true, evalScripts:true, method: 'get'})
The last piece was to add the rjs and have it pull in the shared/_admin_navbar.rhtml partial.
page['admin_bar'].replace_html :partial => 'shared/admin_bar'
And that’s it, now the rjs should pull in your admin_navbar partial and dump it into the admin_bar div.
Aug
7
So I figure if you read this, you must either know me personally or you are a fan of the web 2.0 show. If you aren’t a fan, you can just ignore this one. Chris and I have been hard at work putting together a new website/backend for the show. We wanted something custom and more suited to what we try to do with the show. When we release the new site, it will also allow us to have all the shows listed in iTunes (like it should be!) instead of like 5 episodes.