I know there are already plugin for what they use to call cache busting. The difference with this one is that you don’t need to modify your htaccess file.
To use it you simple add an r to the function, like this:
echo cssr('assets/css/site.css');
or
echo jsr('assets/css/site.css');
It supports the same arguments as the original functions does.
Be aware! Kirby has a bug in 2.3 beta which prevent the css and js to work with arrays. It will probably be fixed soon, but it can break this plugin in Kirby 2.3 beta.
That’s what I would recommend as well. The official cachebuster plugin does it like that as well and it even uses the native css and js functions so that there is no template change needed.
Apart from requiring rewrite rules, is there anything that you don’t like about the cachebuster plugin?
The rewriting solution does have advantages: Some environments don’t cache resources at all if they have a query string (i.e. if they contain a question mark). So including the file modification time in the filename is safer.
I have experienced this with a proxy server and also when using appcache manifests, so I always prefer the rewriting solution from now on.
Regarding the “which solution is better” part: That always depends on the use-case of course. But in my opinion it is essential to use the file modification time instead of the current time, otherwise you could just disable caching completely. Nothing will be cached if you use the current time.
There are other options as well. If you use gulp/gulp-rev, you get files with a hashed suffix and a rev-manifest file from which you can fetch the correct filename without changing the .htaccess. A bit more to write, but no changes to the .htaccess and no changes to the css/js helpers. And the suffix is part of the filename.
The thing I don’t understand about your plugin, @jenstornell, is the signed in users bit. I would want all my users to get the latest files?
like @texnixe mentioned there is a gulp task for hashing your assets like js / css or even image files.
I think the real downside of your @jenstornell plugin is that by using the “time()” function you will disable any browser caching of your asset files? I’m not sure if that is the right thing to do because you want returning visitors to already have your assets in their cach.
For me I prefer to hash the assets via gulp, so when the content changes then my file changes and if the asset stays the same the hash will not change and so the user can get a cached version.
That’s exactly how it should work. In fact, you want to set the assets to never expire and then control cache busting via the hashed file. Using time() to bust the cache on every page load is really counterproductive.
I’ve worked with gulp-rev and gulp-rev-outdated on a big project, not really fond of it but it works (somewhat…) and if you have that kind of technical requirements I guess it helps.
One pitfall with gulp-rev (and any similar approach) is that you’ll probably forgot to use it for parts of your assets, e.g. you used it for CSS and JS but not for your SVG sprites or some of the layout images -> bam, disconnect between your styles or scripts and the files they reference.
For small projects I’m happy with adding a query string to asset URLs, even a manually managed, global one, or a per-file one based on filemtime (as in Get path to assets/images).