views:

221

answers:

4

I'm pretty sure I can page cache the vast majority of my site but the one thing preventing me from doing so is that my flash messages will not show, or they'll show at the wrong time.

One thing I'm considering is writing the flash message to a cookie, reading it and displaying it via javascript and clearing the cookie once the message has been displayed. Has anyone had any success doing this or are there better methods?

Thanks.

A: 

You don't have to cache entire page. Try fragment caching API

Kylo
Maybe he wants to cache the entire page? Page caching gives the biggest performance boost.
John Topley
Yes, correct. I'm already fragment caching at the moment to get around the problem of the flash.
KJF
A: 

One solution would be to cache the page, but include a javascript snippet that will make another small request just for the section you want to be dynamic. So the user will download the page fully, and then when javascript executes, it will pull down the dynamic page element.

I wrote a short blog post about this a while back. http://chase.ratchetsoftware.com/2008/12/rails-caching-dynamic-fragments/

Also, Greg Pollack of RailsEnvy did a screencast where he focuses on having dynamic data in cached pages. http://railslab.newrelic.com/2009/02/05/episode-5-advanced-page-caching

Hope this helps,

Chase Gray

Chase M Gray
I like this idea. Seems like it could be the ticket.
KJF
I think it's worth noting that this won't realise the full benefit of page caching, because the Rails process will still be hit every time instead of not at all. I can't think of a better solution though.
John Topley
Fragment caching is better than serving a second HTTP request that only requests data which could be included in the first one. This strategy would double the number of HTTP requests you serve.
Jesse Dhillon
A: 

I'm dealing with the same problem and I found cacheable-flash plugin that does exactly what KJF described in the question.

I think this is simpler and nicer solution than making excessive ajax calls.

Anton Mironov
A: 

I don't use Rails but this is how I did it in Python using UUIDs:

# set flash messages like this
def flash(self, title, body):
  session['flash_messages'].append({
    'title': title,
    'body': body,
    'uuid': uuid().hex  # stores a UUID as a string
  })

...

self.flash('foo', 'bar')

Then in the base template I have this:

<script type="text/javascript">
  {% for m in session.flash_messages %}
    if(!Cookies.get('{{m.uuid}}')) {
      Notify('{{m.title}}', '{{m.body}}');
      Cookie.set('{{m.uuid}}', 'true', 86400); // key, value, expiry seconds
    }
  {% endfor %}
</script>

I'll break it down for the Pythonically-challenged:

  1. When you add a flash message, you create a unique ID and store it with that message.
  2. Before you display the message, you check to see if a cookie named with the message's unique ID has been set.
  3. If that cookie has not been set, flash the message and set the cookie. Expire the cookie in a day, or as brief as you think is wise.

Now if this page is pulled from cache, it will be okay. At step 2 the test for the cookie will pass because it has already been set, and the message will not be displayed.

Jesse Dhillon