# Page caching + authentication/flash messages = chaos?

**URL:** <https://www.ruby-forum.com/t/page-caching-authentication-flash-messages-chaos/156703>\
**Category:** Rails\
**Created:** [January 1, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/page-caching-authentication-flash-messages-chaos/156703 "2009-01-01T14:10:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [January 1, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/page-caching-authentication-flash-messages-chaos/156703/1 "2009-01-01T14:10:39Z")

</div>

I have run into a bit of a caching problem. I’m building a website that  
mostly consists of static editable pages. There is a basic  
authentication system to restrict who is able to edit the pages and so  
on. If you’re logged in, an edit and a destroy link will show up.

This is my problem: If you’re logged in and a page hasn’t been cached  
yet, it will be cached with edit/destroy links for everyone else to see.  
They can’t edit the pages anyway, so what are they going to need the  
links for? And if the page has been cached by a regular user, the  
administrator can’t see the links.

And also, when the admin presses “Log in”, I want a flash message to  
show up telling him that he has logged in successfully. Again: If the  
page hasn’t been cached yet, the flash message will be visible to  
everyone. If the page wasn’t cached before, the flash messages won’t  
show up.

Does this mean that flash messages and authentication is an impossible  
combination? Is there any way to work around this?

–  
David T…  
[http://twitter.com/datra](http://twitter.com/datra)

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [January 1, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/page-caching-authentication-flash-messages-chaos/156703/2 "2009-01-01T14:39:16Z")

</div>

> Does this mean that flash messages and authentication is an impossible  
> combination? Is there any way to work around this?
> 
> –  
> David T…  
> [http://twitter.com/datra](http://twitter.com/datra)

That’s an easy one. For your case you cannot use page or action caching  
as you have a flash. You can only cache using fragment caching, and you  
have to place your fragments in the correct places otherwise you will  
have the behavior you describe.

Caching is a double edged sword.

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [January 1, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/page-caching-authentication-flash-messages-chaos/156703/3 "2009-01-01T14:44:58Z")

</div>

Fernando P. wrote:

> > Does this mean that flash messages and authentication is an impossible  
> > combination? Is there any way to work around this?

> That’s an easy one. For your case you cannot use page or action caching  
> as you have a flash. You can only cache using fragment caching, and you  
> have to place your fragments in the correct places otherwise you will  
> have the behavior you describe.
> 
> Caching is a double edged sword.

I think you’re right. I have been googling around to find something, and  
it appears to be, that I’m not the only one having these kind of  
problems. I have seen solutions like displaying flash messages with  
Javascript (that requires a 5000 lines Prototype library, though), I’ve  
been reading about conditional caching, clever caching with memcached  
and so forth.

But nothing actually solves the problem without giving new ones. So I  
guess I have to use fragment caching?

–  
David T…  
[http://twitter.com/datra](http://twitter.com/datra)

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [January 1, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/page-caching-authentication-flash-messages-chaos/156703/4 "2009-01-01T19:02:30Z")

</div>

Fernando P. wrote:

> > But nothing actually solves the problem without giving new ones. So I  
> > guess I have to use fragment caching?

> We use fragment caching on our VoD website: [http://www.digiprof.fr](http://www.digiprof.fr) , you  
> cannot do page caching if once the user is authenticated, he gets a  
> custom page.
> 
> PS: yeah Prototype is huge, we are currently considering other lighter  
> options, such as mootools.

It would have been kind of frustrating to have 5000 lines of Javascript  
hanging around just to make one thing little thing work: Cacheable  
Flash. So I don’t want to do that.

I’ll try fragment caching, then. Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [January 2, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/page-caching-authentication-flash-messages-chaos/156703/5 "2009-01-02T17:20:54Z")

</div>

> Does this mean that flash messages and authentication is an impossible  
> combination? Is there any way to work around this?

I am using Cacheable Flash for the Rails Magazine web site and it  
works rather well (you can see it in action on login).

The trick is to page cache only pages for anonymous users (using an  
apache or nginx rewrite rule to detect specific cookies/authenticated  
users).

Including Prototype or other Javascript libraries is not a huge deal  
as these can be cached on the client (so the transfer hit is incurred  
only on the first request). On the other hand it’s overkill to  
include them solely for accessing cookies or dealing with cacheable  
flash.

Best regards,  
Olimpiu

On Jan 1, 1:02 pm, David T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![davidtl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/35a633/32.png) [@davidtl](https://www.ruby-forum.com/u/davidtl)\
**Post date:** [January 1, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/page-caching-authentication-flash-messages-chaos/156703/6 "2009-01-01T17:25:57Z")

</div>

> I think you’re right. I have been googling around to find something, and  
> it appears to be, that I’m not the only one having these kind of  
> problems. I have seen solutions like displaying flash messages with  
> Javascript (that requires a 5000 lines Prototype library, though), I’ve  
> been reading about conditional caching, clever caching with memcached  
> and so forth.
> 
> But nothing actually solves the problem without giving new ones. So I  
> guess I have to use fragment caching?  
> We use fragment caching on our VoD website: [http://www.digiprof.fr](http://www.digiprof.fr) , you  
> cannot do page caching if once the user is authenticated, he gets a  
> custom page.

PS: yeah Prototype is huge, we are currently considering other lighter  
options, such as mootools.

Best regards,
