{"id":552,"date":"2014-06-19T08:17:11","date_gmt":"2014-06-19T08:17:11","guid":{"rendered":"http:\/\/localhost\/__sites\/3d_blog\/?p=552"},"modified":"2014-06-19T20:55:13","modified_gmt":"2014-06-19T20:55:13","slug":"no-more-cache-force-the-reload-based-on-file-date","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=552","title":{"rendered":"no more cache \u2013 force the reload based on the file date"},"content":{"rendered":"<blockquote><p>Goal: avoid cache in case of changed files<\/p><\/blockquote>\n<p>The cache is our friend in case of old files. With this code I can use the cache based on my file date.<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n&lt;link href=&quot;css\/main.css?&lt;?php echo(filemtime(&quot;css\/main.css&quot;)); ?&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; \/&gt;\r\n<\/pre>\n<p>the result is<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n&lt;link href=&quot;css\/main.css?1402529381&quot; rel=&quot;stylesheet&quot; type=&quot;text\/css&quot;&gt;\r\n<\/pre>\n<p>In a little bit friendly shape:<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n...\r\nclass no_cache{\r\n  public static function css($fileName){\r\n    echo('&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;'.self::get_file($fileName).'&quot; media=&quot;screen&quot;\/&gt;');\r\n  }\r\n  public static function js($fileName){\r\n    echo('&lt;script src=&quot;'.self::get_file($fileName).'&quot;&gt;&lt;\/script&gt;');\r\n  }\r\n  public static function file($fileName){\r\n    echo(self::get_file($fileName));\r\n  }\r\n  public static function get_file($fileName){\r\n    return $fileName . '?' . filemtime($fileName);\r\n  }\r\n}\r\n...\r\n&lt;?php \r\n  no_cache::css('css\/main.css'); \r\n  no_cache::js('js\/jquery-1.7.1.js');\r\n  no_cache::js('js\/main.js');\r\n?&gt;\r\n... \r\n\/* and the output is *\/\r\n&lt;link rel=&quot;stylesheet&quot; type=&quot;text\/css&quot; href=&quot;css\/main.css?1403178760&quot; media=&quot;screen&quot;&gt;\r\n&lt;script src=&quot;js\/jquery-1.7.1.js?1368082485&quot;&gt;&lt;\/script&gt;\r\n&lt;script src=&quot;js\/main.js?1402517230&quot;&gt;&lt;\/script&gt;\r\n...\r\n<\/pre>\n<p>Shorter, smarter&#8230;<\/p>\n<p>Call the no_cache class like<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\ncss: no_cache::css('css_file_path');\r\njs: no_cache::js('js_file_path');\r\nfile: no_cache::file('file_path');\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Goal: avoid cache in case of changed files The cache is our friend in case of old files. With this code I can use the cache based on my file date. the result is In a little bit friendly shape: Shorter, smarter&#8230; Call the no_cache class like<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,5],"tags":[],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-php","category-web-trick"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=552"}],"version-history":[{"count":10,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":662,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions\/662"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}