-
Notifications
You must be signed in to change notification settings - Fork 15
03 Cache Busting
Rumen Damyanov edited this page Jul 29, 2025
·
1 revision
This example shows how to implement cache busting to ensure browsers load updated assets.
Cache busting prevents browsers from using cached versions of assets when files have been updated. This ensures users always get the latest version of your CSS and JavaScript files.
Create a JSON file containing asset hashes:
{
"css/app.css": "529e54acf891ccc6592f115afa1cc077",
"js/app.js": "a8b2c3d4e5f6789012345678901234567890",
"css/bootstrap.min.css": "1a2b3c4d5e6f7890123456789abcdef0"
}
<?php
use Rumenx\Assets\Asset;
// Set the cache buster file path
Asset::setCachebuster('/path/to/public/build/assets.json');
// Add your assets normally
Asset::add('css/app.css');
Asset::add('js/app.js');
Asset::add('css/bootstrap.min.css');
// Output with cache busting
echo Asset::css();
echo Asset::js();
<link rel="stylesheet" href="css/app.css?529e54acf891ccc6592f115afa1cc077" />
<link rel="stylesheet" href="css/bootstrap.min.css?1a2b3c4d5e6f7890123456789abcdef0" />
<script src="js/app.js?a8b2c3d4e5f6789012345678901234567890"></script>
If you're using Laravel Mix, you can generate the manifest automatically:
// webpack.mix.js
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.version(); // This generates the manifest
// The manifest will be created at public/mix-manifest.json
Create a simple PHP script to generate hashes:
<?php
$assets = [
'css/app.css' => 'public/css/app.css',
'js/app.js' => 'public/js/app.js',
'css/bootstrap.min.css' => 'public/css/bootstrap.min.css',
];
$manifest = [];
foreach ($assets as $key => $file) {
if (file_exists($file)) {
$manifest[$key] = md5_file($file);
}
}
file_put_contents('public/build/assets.json', json_encode($manifest, JSON_PRETTY_PRINT));
echo "Cache manifest generated successfully!\n";
<?php
use Rumenx\Assets\Asset;
// Only enable cache busting in production
if (getenv('APP_ENV') === 'production') {
Asset::setCachebuster(__DIR__ . '/public/build/assets.json');
}
// Add assets
Asset::add('css/normalize.css');
Asset::add('css/app.css');
Asset::add('js/vendor.js');
Asset::add('js/app.js');
?>
<!DOCTYPE html>
<html>
<head>
<title>Production App</title>
<?= Asset::css() ?>
</head>
<body>
<h1>My Production Application</h1>
<?= Asset::js() ?>
</body>
</html>
If the cache manifest file doesn't exist or an asset isn't found in the manifest, the package will:
- Try to generate a hash using the file's modification time
- Fall back to serving the asset without a cache buster
- Continue working without breaking your application
- Generate manifest during build: Include cache manifest generation in your deployment process
- Use with CDN: Cache busting works great with CDN configurations
- Environment-specific: Only enable in production to avoid development overhead
- Automate updates: Update the manifest whenever assets change