LoginSignup
0
1

More than 5 years have passed since last update.

CSSファイルやJavaScriptファイルの末尾のバージョン番号を追加する方法

Last updated at Posted at 2017-12-06

CSSファイルやJavaScriptファイルを編集した際には、ブラウザはそのファイルの以前のキャッシュを利用しないため、ファイルの末尾にバージョン番号を付与されると、その変更内容がすぐに反映される事となります。

ステップ 1

まず、以下のような独自のヘルパー関数を定義します。

App/Helpers/Version.php
<?php


if (! function_exists('v')) {

    /**
     * [v description]
     * @param  [type]  $path   [description]
     * @param  boolean $secure [description]
     * @return [type]          [description]
     */
    function v($path)
    {
        $path = ltrim($path, '/');

        if (! file_exists(base_path($path))) return "/{$path}";

        $file = "/{$path}?ver" . filemtime(base_path($path));

        return app('url')->asset($file);
    }

}

ステップ 2

次、composer.jsonのautoload.filesへ以下のようにパスを追加します。

composer.json
"autoload": {
    "classmap": [
        ...
    ],
    "psr-4": {
        "App\\": "app/"
    },
    "files": [
        // ↓↓↓ 追加 ↓↓↓
        "app/Helpers/Version.php"
    ]
}

ステップ 3

最後、以下のComposerコマンドを実行します。

composer dump-autoload

以上です。

各場所から自作のヘルパー関数が読み込めるようになっていると思います。

welcome.blade.php

<link rel="stylesheet" href="{{ v('/public/css/app.css') }}">
結果

<link href="http://localhost/demo/public/css/app.css?ver1499187293" rel="stylesheet">
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1