Drongo_III Posted June 26, 2014 Share Posted June 26, 2014 Hi Guys I have recently been looking into implementing browser caching after reading this: https://developers.google.com/speed/docs/best-practices/caching There are a few things in that google article that don't make much sense to me so I'm hoping someone can clarify. Firstly Question The document says: " we recommend that you configure your web server to explicitly set caching headers and apply them to all cacheable static resources" This implies you can set caching headers for just some resources. How would you do this? Lets assume I only wanted to set caching headers for a file styles.css - would I need to output headers within the CSS file and then change the extension to .php? e.g. <link rel="stylesheet" href="styles.php"/> Second question Etags have me very confused. How would you set etag headers for specific files and then generally? And if you set the etag in the header of the main php page does that mean everything gets cached? Any help someone can provide would be very, very welcome! Drongo Quote Link to comment https://forums.phpfreaks.com/topic/289293-cache-headers/ Share on other sites More sharing options...
Jacques1 Posted June 26, 2014 Share Posted June 26, 2014 Managing the cache headers is the job of your webserver. So you should grab the documentation of nginx or Apache or whatever it is you're using and look up how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/289293-cache-headers/#findComment-1483251 Share on other sites More sharing options...
Drongo_III Posted June 29, 2014 Author Share Posted June 29, 2014 Thanks jacques. I'm still confused as to how etags are implemented on a per resource basis. I realise it can be implented through htaccess but how would you define it for a particular resource ? Sorry if I am being slow on this but nothing I read on google leaves me very enlightened. Quote Link to comment https://forums.phpfreaks.com/topic/289293-cache-headers/#findComment-1483395 Share on other sites More sharing options...
requinix Posted June 29, 2014 Share Posted June 29, 2014 You don't set ETag values for individual files. You enable it for a set of files, be that *.* or *.jpg or whatever, and tell the web server how to calculate the value. For example, Apache can calculate the value based on the file modification time and/or size and/or inode. Quote Link to comment https://forums.phpfreaks.com/topic/289293-cache-headers/#findComment-1483398 Share on other sites More sharing options...
kicken Posted June 30, 2014 Share Posted June 30, 2014 (edited) I'm still confused as to how etags are implemented on a per resource basis. I realise it can be implented through htaccess but how would you define it for a particular resource ? If you wanted to set it for a specific file you'd use the <Files> block to limit it's scope, eg: <Files somefile.css> FileETag MTime Size </Files> Edited June 30, 2014 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/289293-cache-headers/#findComment-1483402 Share on other sites More sharing options...
Drongo_III Posted June 30, 2014 Author Share Posted June 30, 2014 (edited) Hi Guys Thanks for sticking with me whilst I try to grasp what is probably an overwhelmingly simple concept...however... This is what has me confused. Excerpt from the google page I referenced: Last-Modified and ETag. These specify some characteristic about the resource that the browser checks to determine if the files are the same. In the Last-Modified header, this is always a date. In the ETag header, this can be any value that uniquely identifies a resource (file versions or content hashes are typical). So what I don't understand is what's the value that "uniquely identifies a resource..." when you place the following in your htaccess? How does the eTag get it's unique identifier? Is it set automatically somehow? <Files somefile.css> FileETag MTime Size </Files> Edited June 30, 2014 by Drongo_III Quote Link to comment https://forums.phpfreaks.com/topic/289293-cache-headers/#findComment-1483437 Share on other sites More sharing options...
kicken Posted June 30, 2014 Share Posted June 30, 2014 The server will generate the ETag based on various file attributes. Which attributes it considers is defined by the FileETag directive. As set in the example, it would use the files last modified date and the size of the file to generate the identifier. How it uses those values to generate the tag is up to it's implementation details. If you were developing a script that needed to generate an ETag you might use something like: $etag = sha1(filemtime($file).'-'.filesize($file)); header('ETag: '.$etag); Quote Link to comment https://forums.phpfreaks.com/topic/289293-cache-headers/#findComment-1483438 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.