Created
June 13, 2019 10:29
-
-
Save kauhat/7a7444457b689ddf0c00e09c887f138e to your computer and use it in GitHub Desktop.
Laravel asset modtime cache busting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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