Laravel/キャッシュ対策:タイムスタンプ付きURL

更新日 1週間前(Asia/Tokyo) 更新者: y963@rogarithm

キャッシュ対策:タイムスタンプ付き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
最終更新: 2025-11-29 09:32:24(Asia/Tokyo)