Change cachable content
How can I change cachable content so that the user will immediately get the refreshed version?
I'll give an example: I have a .css file that is cachable for 2 weeks, so ev开发者_如何学Goen if I change it, users will still get the old version, unless the press F5.
There are a few solutions, that I know of, but none are perfect:
- Changing the filename (from main.css to main2.css).
- Adding a unique URL parameter (like main.css?v=1).
I think that this could somehow be solved using apache eTags and I tried once with that but no luck.
Maybe this is way off, but doesn't bouncing apache clear its cache?
Something like: /usr/sbin/apachectl graceful
perhaps?
Better yet, are you using apache itself for caching, or something else like squid?
I run apache 'normally', and when I make changes to css or other files (outside of scripts), just loading the page in my browser (like by clicking the "home" button), the browser shows the old version. Sometimes this is true even when I press F5. The only way I know to force the browser to truly reload is with Ctrl-Shift-R (on Windows) or Cmd-Shift-R (Mac OS).
I think that the best bet is to use the second option: add a parameter to the URL. So instead of writing css/main.css
, just write css/main.css?v=1
and then increment the v
(as in version) parameter. I see it used all the time.
Sometimes a timestamp is added instead of the version, but be careful not to change this timestamp with every access to the page, as that would result in users never using their cache.
精彩评论