avholodnyak/assets-buster-bundle
Installation:
composer require avholodnyak/assets-buster-bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
AVHolodnyak\AssetsBusterBundle\AssetsBusterBundle::class => ['all' => true],
Configure:
Add to config/packages/assets_buster.yaml (Symfony 4+) or config/config.yml (Symfony 3):
assets_buster:
busters_path: "%kernel.project_dir%/public/busters.json"
version_format: "%s?v=%s" # Optional: Customize version format
Generate Buster File:
Use gulp-buster to generate busters.json:
npm install --save-dev gulp-buster
Example gulpfile.js:
const gulp = require('gulp');
const buster = require('gulp-buster');
gulp.task('buster', () =>
gulp.src(['public/css/*.css', 'public/js/*.js'])
.pipe(buster())
.pipe(gulp.dest('public'))
);
First Use Case: Reference assets in Twig/PHP templates as usual:
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
<script src="{{ asset('js/main.js') }}"></script>
The bundle auto-appends versions (e.g., ?v=abc123) if entries exist in busters.json.
Asset Versioning Pipeline:
gulp buster after asset changes to update busters.json.busters.json during deployment (e.g., via CI/CD).Integration with Asset Pipelines:
gulp-buster alongside your bundler to generate versioned hashes.
Example: Post-build hook in package.json:
"scripts": {
"build": "webpack && gulp buster"
}
AssetMapper for dynamic asset paths:
{{ asset('controller/action', 'css/app.css') }} {# Auto-resolves to public/css/app.css #}
Conditional Versioning:
busters.json for assets that should not bust cache (e.g., third-party CDN assets).busters.json:
{
"css/app.css": "abc123",
"js/main.js": "def456"
// "vendor/jquery.js": null {# No version appended #}
}
Environment-Specific Config:
Override busters_path per environment (e.g., config/packages/dev/assets_buster.yaml):
assets_buster:
busters_path: "%kernel.project_dir%/public/busters-dev.json"
busters.json only for changed files to minimize rebuilds.busters.json:
php bin/console cache:clear --env=prod
version_format: "%s?debug=%s" to verify versions are being read.File Path Mismatches:
busters_path must point to the root-relative path of busters.json (e.g., public/busters.json).
Using web/busters.json (Symfony 3 default) may fail if assets are served from public/.busters.json match the public URL of assets (e.g., "css/app.css" not "web/css/app.css").Symfony 4+ Configuration:
config.yml in Symfony 3. In Symfony 4+, use config/packages/assets_buster.yaml.config/bundles.php.Version Format Quirks:
version_format (e.g., "%s#%s") may break asset linking if not URL-encoded."%s?v=%s" or ensure the format is URL-safe.Missing Buster Entries:
busters.json render without versions, which can cause stale cache issues.busters.json (even with placeholder versions).Gulp-Buster Compatibility:
gulp-buster may not handle nested directories or symlinked assets correctly.gulp-buster explicitly:
gulp.src(['public/**/*.{css,js}'], { base: 'public' })
.pipe(buster())
.pipe(gulp.dest('public'));
busters.json is readable and contains valid JSON:
cat public/busters.json
framework:
profiler: { only_exceptions: false }
Dynamic Buster Paths:
Override the BustersPathResolver service to fetch busters.json from an API or database:
services:
AVHolodnyak\AssetsBusterBundle\Resolver\BustersPathResolver:
arguments:
- "@request_stack" # Example: Fetch path from request
Custom Version Strategies:
Extend the bundle’s BusterVersionStrategy to support additional version sources (e.g., Git commits):
class GitVersionStrategy extends BusterVersionStrategy {
public function getVersion(string $path): ?string {
return shell_exec("git rev-parse HEAD");
}
}
Register it in services.yaml:
AVHolodnyak\AssetsBusterBundle\VersionStrategy\VersionStrategyInterface:
class: App\Strategy\GitVersionStrategy
Asset Filtering:
Exclude specific assets (e.g., vendor/) from versioning by creating a custom AssetFilter:
class VendorAssetFilter implements AssetFilterInterface {
public function shouldVersion(string $path): bool {
return !str_starts_with($path, 'vendor/');
}
}
Bind it in services.yaml:
AVHolodnyak\AssetsBusterBundle\AssetsBusterBundle:
arguments:
$assetFilter: '@App\Filter\VendorAssetFilter'
How can I help you explore Laravel packages today?