javascript - set cache headers only when something changed in the website (php, htaccess) -


i'm running wordpress. added code in .htaccess file users see latest version of website , not cached one.

<ifmodule mod_rewrite.c>     header set cache-control "no-cache, no-store, must-revalidate"     header set pragma "no-cache"     header set expires 0 </ifmodule> 

but loads , reason clear. cause every time when refreshing, loads new files again. wondering there other way detect when had been changed, add these headers or clear cache in other way. don't want use plugin. want fix via php or javascript code. appreciated.

you can set expires headers different file extensions so:

<ifmodule mod_expires.c>   expiresactive on    # document html   expiresbytype text/html "access plus 0 seconds"    # media: images, video, audio   expiresbytype audio/ogg "access plus 1 month"   expiresbytype image/gif "access plus 1 month"   expiresbytype image/jpeg "access plus 1 month"   expiresbytype image/png "access plus 1 month"   expiresbytype video/mp4 "access plus 1 month"   expiresbytype video/ogg "access plus 1 month"   expiresbytype video/webm "access plus 1 month"    # css , javascript   expiresbytype application/javascript "access plus 1 year"   expiresbytype text/css "access plus 1 year" </ifmodule> 

for specific files know changed on more regular basis, can add extension such ?=[timestamp] url, browser recognises new version.

for example, change .js file main.js?version=2 when want viewer see new version.

alternatively can change extension main.js?ts=<?php echo date() ?> file reloaded on every visit, because timestamp different each time.


edit solution use last-edited time of file (using filemtime()) append parameter, so:

<script type='text/javascript' src='main.js?fmt=<?= filemtime("main.js") ?>'> <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); echo '?ver=' . filemtime( get_stylesheet_directory() . '/style.css'); ?>" type="text/css" media="all" /> 

this mean first load after change in file's data force refresh.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -