Laravel/キャッシュ対策:タイムスタンプ付きURL
キャッシュ対策:タイムスタンプ付きURL
基本的な考え方
<script src="{{ asset('publicからのファイル相対パス') }}?v={{ filemtime(public_path('publicからのファイル相対パス')) ">
関数化すると
// 下記をファイルに書いておいて、index.phpなどでrequireする?
if (! function_exists('assetWithUpdateQuery')) {
function assetWithUpdateQuery($path)
{
if (is_null($path)) {
return null;
}
if (file_exists(public_path($path))) {
return asset($path).'?v='.filemtime(public_path($path));
}
return asset($path);
}
}
使いかた
<script src="{{ assetWithUpdateQuery('publicからのファイル相対パス') }}">
Laravel/キャッシュ対策:タイムスタンプ付きURL.md