最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I prevent javascript caching? Querystring approach isn't working - Stack Overflow

programmeradmin1浏览0评论

I've seen other approaches that attach a version number or MD5 hash to a JS src querystring.

e.g. <script src='/script/v1/'></script>

However, my JavaScript is still getting cached in multiple browsers (Chrome, Firefox) when I push a new version of my site.

This seems like a major problem that others have solved, and I seem to be doing the right things. How can I get this to work?

I've seen other approaches that attach a version number or MD5 hash to a JS src querystring.

e.g. <script src='/script/v1/'></script>

However, my JavaScript is still getting cached in multiple browsers (Chrome, Firefox) when I push a new version of my site.

This seems like a major problem that others have solved, and I seem to be doing the right things. How can I get this to work?

Share Improve this question edited Apr 4, 2011 at 17:44 marclar asked Apr 4, 2011 at 16:20 marclarmarclar 3,0465 gold badges37 silver badges57 bronze badges 5
  • What server do you have? – Shadow Wizzard Commented Apr 4, 2011 at 16:54
  • These static files are served from lighttpd – marclar Commented Apr 4, 2011 at 17:50
  • 1 Then I fear the files are cached in the server memory.. here is something that led me in this direction: redmine.lighttpd/wiki/1/Docs:ModMemCache – Shadow Wizzard Commented Apr 4, 2011 at 21:24
  • Anyone knows if query string appending is reliable? i.e. /script.js?v=1.1 I usually use here /script_timestamp.js but would prefer the querystring approach (looks cleaner to me). – turtlepick Commented Jun 27, 2012 at 23:14
  • @flaviotsf - look at my answer below, specifically the reference to Steve Souders' blog. – marclar Commented Jun 28, 2012 at 14:23
Add a ment  | 

3 Answers 3

Reset to default 7

I added log messages and determined that the querystring method is working. Sorry for the unnecessary question.

However, in researching, I found some important points worth mentioning:

  1. One of the articles suggests using a querystring with the current time appended. You probably don't want to follow this suggestion as your files will never be cached. Using source control version numbers or an MD5 hash would be better.
  2. Steve Souders (of High Performance Web Sites fame) notes that certain web proxies never cache anything with a querystring. Thus, the version number should be embedded within the path to the file in order to ensure that your files are cached appropriately when accessed through these proxies. ( http://www.stevesouders./blog/2008/08/23/revving-filenames-dont-use-querystring/ )

It will be cached always. Although, by using a version number (or any other varying string) a new version will be downloaded and used every time, ignoring the previous one.

http://thecrmgrid.wordpress./2007/10/22/prevent-caching-of-javascript-include-files-during-development/

http://davidwalsh.name/prevent-cache

1.)make sure the response headers for the javascript files are correct and include expires, cache-control, etc.

2.)you probably have to append the version not as a query parameter but part of the filename, e.g. page_v.2.js. You could change the javascript filenames at build time for example if you are using Java. That is what i have done.

发布评论

评论列表(0)

  1. 暂无评论