Stamp for Craft
A tiny plugin for adding timestamp to filenames., (*1)
This is the Craft 2.x version of Stamp, for the Craft 3.x version see the craft3 branch., (*2)
Usage
Use it like this:, (*3)
<script src="{{ craft.stamp.er('/assets/build/js/scripts.js') }}"></script>
Which results in:, (*4)
<script src="/assets/build/js/scripts.1399647655.js"></script>
The er() method takes a second parameter for setting the format of the output. Possible values are file
(default), folder
, query
and tsonly
., (*5)
Example with folder
:, (*6)
<script src="{{ craft.stamp.er('/assets/build/js/scripts.js', 'folder') }}"></script>
Result:, (*7)
<script src="/assets/build/js/1399647655/scripts.js"></script>
Example with query
:, (*8)
<script src="{{ craft.stamp.er('/assets/build/js/scripts.js', 'query') }}"></script>
Result:, (*9)
<script src="/assets/build/js/scripts.js?ts=1399647655"></script>
Example with tsonly
:, (*10)
Timestamp is: {{ craft.stamp.er('/assets/build/js/scripts.js', 'tsonly') }}
Result:, (*11)
Timestamp is: 1399647655
Hashing option
The er() method takes a third parameter for setting the algorithm of the output. Possible values are ts
(default), and hash
., (*12)
ts
stands for timestamp and behaves as shown above. hash
gets the CRC32 checksum of the file instead of the timestamp. It's useful for cases when you need your cache busting to be fully deterministic., (*13)
For example:, (*14)
<script src="{{ craft.stamp.er('/assets/build/js/scripts.js', 'file', 'hash') }}"></script>
Result:, (*15)
<script src="/assets/build/js/scripts.2031312059.js"></script>
Note: For the sake of consistency, calling er('file', 'tsonly', 'hash')
will return just the hash, despite the fact that tsonly
starts with ts
., (*16)
URL rewriting
For methods file
and folder
you probably want to do some url rewriting. Below are some examples of how this can be done,
adjust as needed for your server and project setup., (*17)
Apache:, (*18)
# Rewrites asset versioning, ie styles.1399647655.css to styles.css.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.(\d{10})\.(js|css)$ $1.$3 [L] # /assets/build/js/scripts.1399647655.js
# RewriteRule ^(.+)/(\d{10})/(.+)\.(js|css)$ $1/$3.$4 [L] # /assets/build/js/1399647655/scripts.js
nginx:, (*19)
location @assetversioning {
rewrite ^(.+)\.[0-9]+\.(css|js)$ $1.$2 last; # /assets/build/js/scripts.1399647655.js
# rewrite ^(.+)/([0-9]+)/(.+)\.(js|css)$ $1/$3.$4 last; # /assets/build/js/1399647655/scripts.js
}
location ~* ^/assets/.*\.(?:css|js)$ {
try_files $uri @assetversioning;
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
Configuration
Stamp needs to know the public document root to know where your files are located. By default
Stamp will use $_SERVER['DOCUMENT_ROOT'], but on some server configurations this is not the correct
path. You can configure the path by setting the stampPublicRoot setting in your config file
(usually found in /craft/config/general.php), (*20)
Example
'stampPublicRoot' => '/path/to/website/public/',
Changelog
Version 1.1
- Added additional parameter to output filepaths in different formats
Version 1.0