<p>I tend to use a config.php variable (called assetVersion) for the production site, and just generate a random value for the dev site using Twig's random function. Looks like this:</p>

<pre><code>&lt;link rel="stylesheet" href="/assets/css/style-min.css{{ craft.config.devMode ? '?v=' ~ random() : '?v=' ~ craft.config.assetVersion }}"&gt;
</code></pre>

CC BY-SA 4.0
2020-08-22T03:59:14.420Z
Nice, clean and simple.
Crafty Cat
CC BY-SA 3.0
2016-11-20T19:41:52.940Z
2016-11-20T19:41:52.940Z
Ian Ebden
<p>This is also possible when using github actions as a deployment tool:</p>
<p>if you have the link like this:</p>
<pre><code>&lt;link href=&quot;/theme/css/styles.css?v=TAG&quot; rel=&quot;stylesheet&quot;&gt; 
</code></pre>
<p>you can add this to your GitHub action:</p>
<pre><code>- name: Invalidate Stylesheet
        run: sed -i &quot;s/?v=TAG/?v=$(date +%s)/g&quot; $GITHUB_WORKSPACE/path/to/file.twig
</code></pre>

CC BY-SA 4.0
2022-08-03T13:04:46.807Z
2022-08-03T13:04:46.807Z
KSPR
<p>Funny that you mention DeployBot — I actually wrote a post at DeployBot's blog about solving this exact problem with Craft, DeployBot, and Gulp.js: <a href="https://deploybot.com/blog/adding-fingerprints-to-assets-with-gulp-and-deploybot" rel="nofollow noreferrer">https://deploybot.com/blog/adding-fingerprints-to-assets-with-gulp-and-deploybot</a></p>

<p>This approach doesn't require any Craft plugins, but assumes that you use a task runner like Gulp or Grunt for building your assets.</p>

CC BY-SA 3.0
2016-11-21T17:09:34.620Z
2016-11-21T17:09:34.620Z
efedorenko
<p>There are several plugins that can do this. </p>

<p>With my plugin <a href="https://github.com/aelvan/Stamp-Craft" rel="nofollow noreferrer">Stamp</a>, you can add a timestamp to the filename or path in the following formats:</p>

<pre><code>&lt;script src="/assets/build/js/1399647655/scripts.js"&gt;&lt;/script&gt;
&lt;script src="/assets/build/js/scripts.1399647655.js"&gt;&lt;/script&gt;
&lt;script src="/assets/build/js/scripts.js?ts=1399647655"&gt;&lt;/script&gt;
</code></pre>

<p>The last one, which is the one you're looking for is made with:</p>

<pre><code>&lt;script src="{{ craft.stamp.er('/assets/build/js/scripts.js', 'query') }}"&gt;&lt;/script&gt; 
</code></pre>

<p>You can also get only the timestamp, and use it however you want, like this:</p>

<pre><code>{% set timestamp = craft.stamp.er('/assets/build/js/scripts.js', 'tsonly') %} 
</code></pre>

<p>It's worth noting that you'd ideally want to not use a query string for this, as various frontside proxies could ignore it. But, you need to be able to set up the proper url rewriting in your webserver for the filename and folder versions, so that might not always be possible.</p>

<p>There is also <a href="https://github.com/focuslabllc/craftcms-cachebuster" rel="nofollow noreferrer">Cache Buster</a> from Focuslab which can add a query string, but also supports file revisioning based on a manifest file. </p>

<p>And there's <a href="https://github.com/clubstudioltd/craft-asset-rev" rel="nofollow noreferrer">Asset Rev</a> from Club Studio which is primarily for file revisioning based on a manifest file, but falls back to a query string. </p>

CC BY-SA 3.0
2016-11-12T16:21:33.537Z
Thanks so much for this André! I will definitely use your plugin.
Magnus
CC BY-SA 3.0
2016-11-12T15:52:32.223Z
2016-11-12T16:37:08.763Z
2016-11-12T16:37:08.763Z
André Elvan
<p>Is there any simple way to automatically add a version number when referencing a CSS file?</p>

<p>Example: <code>&lt;link href="style.css?v=VERSION-HERE" rel="stylesheet" type="text/css"&gt;</code></p>

<p>Version could be a timestamp when the style.css was last modified or something like that. </p>

<p>The reason for this is that I often have to tell my clients to force the browser to reload because they don't see the latest changes I've made in style.css. </p>

<p>If it matters, I use Deploybot and github.</p>

<cache><css>
Force browser to download latest CSS file
# Force browser to download latest CSS file

(Asked by: username_0 on 2016-11-12)

Is there any simple way to automatically add a version number when referencing a CSS file?

Example:

Version could be a timestamp when the style.css was last modified or something like that.

The reason for this is that I often have to tell my clients to force the browser to reload because they don't see the latest changes I've made in style.css.

If it matters, I use Deploybot and github.

**Tags:** cache, css

---

## 4 answers

### Answer by username_5 on 2016-11-12. Score: 7 | Accepted answer

There are several plugins that can do this. 
With my plugin Stamp, you can add a timestamp to the filename or path in the following formats:




The last one, which is the one you're looking for is made with:
 

You can also get only the timestamp, and use it however you want, like this:
{% set timestamp = craft.stamp.er('/assets/build/js/scripts.js', 'tsonly') %} 

It's worth noting that you'd ideally want to not use a query string for this, as various frontside proxies could ignore it. But, you need to be able to set up the proper url rewriting in your webserver for the filename and folder versions, so that might not always be possible.
There is also Cache Buster from Focuslab which can add a query string, but also supports file revisioning based on a manifest file. 
And there's Asset Rev from Club Studio which is primarily for file revisioning based on a manifest file, but falls back to a query string.

> **Comments:**
> **username_0**: Thanks so much for this André! I will definitely use your plugin.  

---

### Answer by username_1 on 2016-11-20. Score: 6

I tend to use a config.php variable (called assetVersion) for the production site, and just generate a random value for the dev site using Twig's random function. Looks like this:

> **Comments:**
> **username_2**: Nice, clean and simple.  

---

### Answer by username_3 on 2022-08-03. Score: 0

This is also possible when using github actions as a deployment tool:
if you have the link like this:
 

you can add this to your GitHub action:
- name: Invalidate Stylesheet
        run: sed -i "s/?v=TAG/?v=$(date +%s)/g" $GITHUB_WORKSPACE/path/to/file.twig


---

### Answer by username_4 on 2016-11-21. Score: 0

Funny that you mention DeployBot — I actually wrote a post at DeployBot's blog about solving this exact problem with Craft, DeployBot, and Gulp.js: <https://deploybot.com/blog/adding-fingerprints-to-assets-with-gulp-and-deploybot>

This approach doesn't require any Craft plugins, but assumes that you use a task runner like Gulp or Grunt for building your assets.


---

