LoginSignup
9
7

More than 5 years have passed since last update.

キャッシュ対策 [自分用メモ]

Last updated at Posted at 2015-05-22

自分用のメモです。
http://doop-web.com/blog/archives/1182

このままでは動かなかったので以下のように修正して使っています。

CSSやJSファイルのタイムスタンプを利用してパラメーターを生成して、呼び出し先のURLにパラメーターを追加する。

ファイルを更新するとタイムスタンプが変わり呼び出しのURLについたパラメーターが変更されるので、キャッシュが使われなくなる。

function cacheBuster($filename) {
 if (file_exists($filename)) {
  echo date('YmdHis', filemtime($filename));
 } else {
  echo 'file not found';
 }
}
<link rel="stylesheet" href="/css/stylesheet.css?date=<? cacheBuster(dirname(__FILE__).'/css/stylesheet.css'); ?>">

設定しておくと、とても便利なのでオススメです。

9
7
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
9
7