views:

82

answers:

2

I'm page caching the majority of my pages and using a dynamic js file to inject the user specific content.

So in my header file I have something like this:

<%= javascript_include_tag '/dynamic_header/current' %>

What this will do is execute the dynamic_header controller show.js.erb view which gets returned alongside the page and handles the injection of the dynamic content.

All well and good until I test under safari. (Firefox works fine)

It seems safari caches this file far too heavily and doesn't notice when it changes, which is every time there is a new flash messages or when a user logs in or out.

Is there an easy way in my controller to add an expires header to this file? Or do people have any other suggestions on how to make safari notice that the cached file has changed.

Thanks.

A: 

Maybe you can add this header:

Cache-Control: no-cache

The best way to do this is probably hooking into rack

Pablo Fernandez
+1  A: 

Normally static files have their modified date appended to the end of them to fix caching issues. Since your file is dynamic you should just add the current time to the end of the path.

This is not supported by javascript_include_tag, so you will need to write the script include tag by hand like so:

<script type="text/javascript" src="/dynamic_header/current?t=<%=Time.now.to_i%>"></script>

Good luck!

Gdeglin
Doesn't rails already do that in dev mode?
Pablo Fernandez
No. The cache breaker is used in both dev and production for all static files. Specifically, when you use a tag like javascript_include_tag or stylesheet_tag rails will search inside of public/stylesheets or public/javascripts for the file so it can append the timestamp to the end of the asset url. If the file does not exist (like in this case since the file is actually dynamically generated) then this step is skipped.
Gdeglin