KJF
KJF

Reputation: 2093

Safari caching pages too heavily, how to add expires header using rails

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.

Upvotes: 0

Views: 626

Answers (2)

Gdeglin
Gdeglin

Reputation: 12618

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!

Upvotes: 2

Pablo Fernandez
Pablo Fernandez

Reputation: 105220

Maybe you can add this header:

Cache-Control: no-cache

The best way to do this is probably hooking into rack

Upvotes: 0

Related Questions