├── cachebuster.php ├── lib ├── css.php └── js.php ├── package.json └── readme.md /cachebuster.php: -------------------------------------------------------------------------------- 1 | __DIR__ . DS . 'lib' . DS . 'css.php', 7 | 'kirby\\cachebuster\\js' => __DIR__ . DS . 'lib' . DS . 'js.php' 8 | ]); 9 | 10 | kirby()->set('component', 'css', 'Kirby\\Cachebuster\\CSS'); 11 | kirby()->set('component', 'js', 'Kirby\\Cachebuster\\JS'); 12 | -------------------------------------------------------------------------------- /lib/css.php: -------------------------------------------------------------------------------- 1 | 11 | * @license MIT 12 | * @link https://getkirby.com 13 | */ 14 | class CSS extends \Kirby\Component\CSS { 15 | 16 | /** 17 | * Builds the html link tag for the given css file 18 | * 19 | * @param string $url 20 | * @param null|string $media 21 | * @return string 22 | */ 23 | public function tag($url, $media = null) { 24 | 25 | if(is_array($url)) { 26 | $css = array(); 27 | foreach($url as $u) $css[] = $this->tag($u, $media); 28 | return implode(PHP_EOL, $css) . PHP_EOL; 29 | } 30 | 31 | $file = kirby()->roots()->index() . DS . $url; 32 | 33 | if(file_exists($file)) { 34 | $mod = f::modified($file); 35 | $url = dirname($url) . '/' . f::name($url) . '.' . $mod . '.css'; 36 | } 37 | 38 | return parent::tag($url, $media); 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /lib/js.php: -------------------------------------------------------------------------------- 1 | 11 | * @license MIT 12 | * @link https://getkirby.com 13 | */ 14 | class JS extends \Kirby\Component\JS { 15 | 16 | /** 17 | * Builds the html script tag for the given javascript file 18 | * 19 | * @param string $src 20 | * @param boolean async 21 | * @return string 22 | */ 23 | public function tag($src, $async = false) { 24 | 25 | if(is_array($src)) { 26 | $js = array(); 27 | foreach($src as $s) $js[] = $this->tag($s, $async); 28 | return implode(PHP_EOL, $js) . PHP_EOL; 29 | } 30 | 31 | $file = kirby()->roots()->index() . DS . $src; 32 | 33 | if(file_exists($file)) { 34 | $mod = f::modified($file); 35 | $src = dirname($src) . '/' . f::name($src) . '.' . $mod . '.js'; 36 | } 37 | 38 | return parent::tag($src, $async); 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cachebuster", 3 | "description": "Kirby Cachebuster Plugin", 4 | "author": "Bastian Allgeier ", 5 | "version": "2.1.0", 6 | "type": "kirby-plugin", 7 | "license": "MIT" 8 | } 9 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Kirby 2 Cachebuster Plugin 2 | 3 | This plugin will add modification timestamps to your css and js files, 4 | as long as they are embedded with the css() and js() helpers. 5 | 6 | ## Requirements 7 | 8 | This plugin requires Kirby 2.3. Older Kirby 2 versions are supported by version 2.0.0 of this plugin. 9 | 10 | Please note that this plugin doesn't add caching headers to your CSS and JS files. 11 | To make proper use of this plugin, you need to add caching rules to your server configuration. 12 | 13 | ## Installation 14 | 15 | To use this plugin, add the cachebuster.php to `site/plugins`. 16 | After that you must add rules to your htaccess file or your nginx configuration (see below). 17 | 18 | Now you can activate the plugin with following line in your `config.php`. 19 | 20 | ``` 21 | c::set('cachebuster', true); 22 | ``` 23 | 24 | ### htaccess rules for Apache 25 | 26 | To make this plugin work on Apache you must add the following lines to your 27 | htaccess file: 28 | 29 | ``` 30 | RewriteCond %{REQUEST_FILENAME} !-f 31 | RewriteRule ^(.+)\.(\d+)\.(js|css)$ $1.$3 [L] 32 | ``` 33 | 34 | Place them directly after the RewriteBase definition. 35 | 36 | ### Nginx rewrite rules 37 | 38 | For Nginx you can add the following to your virtual host setup: 39 | 40 | ``` 41 | location /assets { 42 | if (!-e $request_filename) { 43 | rewrite ^/(.+)\.(\d+)\.(js|css)$ /$1.$3 break; 44 | } 45 | } 46 | ``` 47 | 48 | ## Authors 49 | 50 | Bastian Allgeier & Lukas Bestle 51 | --------------------------------------------------------------------------------