开发者

How do I implement a cache buster , if I use git? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers. 开发者_开发百科

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 7 years ago.

Improve this question
<link rel="/style/style.css"/>

So this is my index.html.

If I save it, I want to automatically implement style.css?version=3 ...so this busts the cache

What's the easiest way to do that?


If you use git, you can use git describe --tags --always to get the current version.

If in PHP, you can do something like :

$currentVersion = `git describe --tags --always`

An then us $currentVersion as a cache buster :

<link href="/style/style.css?version=<?=$currentVersion?>">

Of course :

  • you should cache the result of the command, at least for one minute, to prevent intensive calls to the git command !
  • be sure that the command is launched in a GIT directory

It will work even in you don't use tags (thanks to the --always switch), but it's nicer to use them, so $currentVersion would be something like v2.1.3-c1a2f3e.


using javascript you can just write

<script type="text/javascript">
     document.write('<link rel="stylesheet" type="text/css" href="style.css?version=' + (new Date()).getTime() + '">');
</script>

Can easier be done if you use any server-side scripting. Something like `

<link href="/style/style.css?version=<?php echo time() ?>">`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜