Skip to content

Instantly share code, notes, and snippets.

@kauhat
Created June 13, 2019 10:29
Show Gist options
  • Save kauhat/7a7444457b689ddf0c00e09c887f138e to your computer and use it in GitHub Desktop.
Save kauhat/7a7444457b689ddf0c00e09c887f138e to your computer and use it in GitHub Desktop.
Laravel asset modtime cache busting
<?php
if (! function_exists('asset_busted')) {
/**
* Generate an asset path with modification time query string.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
function asset_busted($path, $secure = null): string
{
if (file_exists(public_path($path))) {
$time = filemtime(public_path($path));
return app('url')->asset($path . '?mtime=' . $time, $secure);
} else {
// Standard asset() fallback
return app('url')->asset($path, $secure);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment