How to Use a Custom Version Strategy for Assets
Asset versioning is a technique that improves the performance of web applications by adding a version identifier to the URL of the static assets (CSS, JavaScript, images, etc.) When the content of the asset changes, its identifier is also modified to force the browser to download it again instead of reusing the cached asset.
If your application requires advanced versioning, such as generating the version dynamically based on some external information, you can create your own version strategy.
Note
Symfony provides various cache busting implementations via the version, version_format, and json_manifest_path configuration options.
Creating your Own Asset Version Strategy
Symfony provides a strategy to version assets based on their contents hash. The following example extends that idea to create a version strategy that reads a JSON file mapping each asset to its content hash and combines that hash with a global application version.
This way, asset URLs change whenever a file's contents change and on every new
release of the application. This file (called hashes.json in this example)
can be generated by your asset build tool:
1 2 3 4
{
"js/script.js": "f9c7afd05729f10f55b689f36bb20172",
"css/style.css": "91cd067f79a5839536b46c494c4272d8"
}
Implement VersionStrategyInterface
Asset version strategies are PHP classes that implement the VersionStrategyInterface. In this example, the constructor of the class takes as arguments the path to the manifest file, the current application version, and the format of the generated version string:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
// src/Asset/VersionStrategy/JsonHashVersionStrategy.php
namespace App\Asset\VersionStrategy;
use Symfony\Component\Asset\VersionStrategy\VersionStrategyInterface;
class JsonHashVersionStrategy implements VersionStrategyInterface
{
private string $format;
/**
* @var string[]
*/
private array $hashes;
public function __construct(
private string $manifestPath,
private string $appVersion,
?string $format = null,
) {
$this->format = $format ?: '%s?%s';
}
public function getVersion(string $path): string
{
if (!isset($this->hashes)) {
$this->hashes = $this->loadManifest();
}
if (!isset($this->hashes[$path])) {
return '';
}
// combine the global application version with the file content hash,
// so the URL changes both on every release and whenever the file changes
return $this->appVersion.'.'.$this->hashes[$path];
}
public function applyVersion(string $path): string
{
$version = $this->getVersion($path);
if ('' === $version) {
return $path;
}
return sprintf($this->format, $path, $version);
}
private function loadManifest(): array
{
return json_decode(file_get_contents($this->manifestPath), true);
}
}
Register the Strategy Service
After creating the strategy PHP class, register it as a Symfony service.
1 2 3 4 5 6 7
# config/services.yaml
services:
App\Asset\VersionStrategy\JsonHashVersionStrategy:
arguments:
- "%kernel.project_dir%/hashes.json"
- "%env(APP_VERSION)%"
- "%%s?version=%%s"
The APP_VERSION environment variable holds the current application version
(e.g. the Git commit hash or the release tag). Define it during your deployment
process so it changes with every new release.
Finally, enable the new asset versioning for all the application assets or just for some asset package thanks to the version_strategy option:
1 2 3 4 5
# config/packages/framework.yaml
framework:
# ...
assets:
version_strategy: 'App\Asset\VersionStrategy\JsonHashVersionStrategy'