Recently I was working with Assetic for combining and minifying CSS and JS files but ran into an issue with regards to caching the created asset collection. The resulting minified file was being rewritten on every page load which was no good at all.
TL;DR: Get the asset collection’s “last modified” property and compare it to the written minified file to determine if it needs updating.
Updated Feb 29, 2016
This post originally used String Assets to cache the collection’s content. It
was later found that the string asset caches would conflict due to string assets
not having “source” or “target” paths which are a component of the hash. Though
an arbitrary source could be set on a string asset to resolve the issue, it’s
probably better to just use the collection’s getLastModified
before writing
the minified file. The post has been updated to reflect the new approach.
If you still want to cache a collection, store the contents in a string asset but be sure to at least set source of the string asset to something unique for the collection. This will ensure that if you have multiple collections generated on page load, they won’t conflict.
|
|
Original Post
Rundown of how to minify, combine, and cache assets with Assetic
- Loop through individual assets (CSS or JS files)
- Apply filters to minify them
- Cache the result so you don’t have to minify them again until the source file changes
- Add the asset to an asset collection
- Finally, write the minified file if either of the following is true
- The minified file does not exist
- The asset collection’s last modified is newer than that of the minified file on the system
- This indicates that the minified file is now out of date from what’s been cached
- This was the key to ensure the minified file wasn’t updated on every page load
The resulting file will be a combination of all minified assets which is only updated when one of the individual assets are updated.
The Code
The following assumes you have an array of file paths. It will write a minified file to the public directory of the site which is only later updated if any of the individual assets have changed.
|
|
There you go! You should now have a minified file which only updates when any of its individual assets are updated. Here’s hoping this saves just one person some time and as always, let me know if there’s a better approach out there! :)