Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/codemonauts/craft-asset-autoversioning
A Twig extension for Craft CMS that helps you cache-bust your assets
https://github.com/codemonauts/craft-asset-autoversioning
cache-bust caching cms craft craftcms plugin
Last synced: about 2 months ago
JSON representation
A Twig extension for Craft CMS that helps you cache-bust your assets
- Host: GitHub
- URL: https://github.com/codemonauts/craft-asset-autoversioning
- Owner: codemonauts
- License: mit
- Created: 2018-08-01T13:54:07.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-05-04T06:33:49.000Z (over 2 years ago)
- Last Synced: 2024-09-18T18:08:51.563Z (4 months ago)
- Topics: cache-bust, caching, cms, craft, craftcms, plugin
- Language: PHP
- Homepage:
- Size: 45.9 KB
- Stars: 4
- Watchers: 5
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Assets Autoversioning plugin for Craft CMS
![Icon](resources/autoversioning.png)
A really basic Twig extension for CraftCMS that helps you cache-bust your assets.
## Background
To force the browser to download the new asset file after a update, the plugin allows you to add a Twig function, which adds the build number or the filemtime to the filename.
## Requirements
* Craft CMS >= 4.0.0
## Installation
### ProjectOpen your terminal and go to your Craft project:
``` shell
cd /path/to/project
composer require codemonauts/craft-asset-autoversioning
./craft install/plugin craft3-assets-autoversioning
```### CI/CD Pipeine
The build number which gets added to the asset URL is read from a file called `build.txt` which must exist in your project folder. Use for example something like this in your deployment script (Example is for CodeShip):``` shell
echo -n "${CI_BUILD_NUMBER}" > build.txt
```If the file doesn't exists, the filemtime of the asset is used.
### Webserver
Because the clients will from now on start to request files like `/css/styles.12345678.css` we need to tell the webserver how to rewrite these URLs so that the original `/css/styles.css` will get served.
**Apache**
``` apacheconfig
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?\/)*?([a-z\.\-]+)(\d+)\.(bmp|css|cur|gif|ico|jpe?g|js|png|svgz?|webp|webmanifest)$ $1$2$4 [L]```
**NGINX**
``` nginx
location ~* (.+)\.(?:\d+)\.(js|css|png|jpg|jpeg|gif|webp)$ {
try_files $uri $1.$2;
}
```## Usage
Use the new Twig function ```versioning()``` in your template. For example in pug:
``` twig
```
will result in something like this:
``` html
```
With ❤ by [codemonauts](https://codemonauts.com)