[CakePHP4] javascriptやcssにタイムスタンプ(更新日時)をつける方法

ブラウザがキャッシュしたJavaScritpやCSSや画像などを確実に更新したい場合、ファイルの後ろにタイムスタンプ(更新日時)をつける。

CakePHP3,CakePHP4ともに以下の方法をとる。

ヘルパーを使って静的ファイルを配置する。

ヘルパーを使わないと、面倒見てくれないので、ヘルパーを使って配置する。

config/app.phpのAsset timestapをtrueかforceにする。

trueはデバックモード時に、forceは常にタイムスタンプをつけるようになる。

結果

ちなみに、UrlHelperの中のassetTimestampメソッド中を見ると、タイムスタンプはファイルのfilemtimeの値を使用している。

https://github.com/cakephp/cakephp/blob/master/src/View/Helper/UrlHelper.php

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">