Book Image

Instant PageSpeed Optimization

By : Sanjeev Jaiswal
Book Image

Instant PageSpeed Optimization

By: Sanjeev Jaiswal

Overview of this book

PageSpeed is an important aspect of web design and site management. It is a Google measure of how well the site performs to technical measurements. PageSpeed can be measured using Google's own tool or a browser plugin. It is used to score sites in indices, and is important from a UI view as it forms a large part of the success of your site.Instant PageSpeed Optimization is a practical, hands-on guide that provides you with a number of clear, step-by-step exercises, which will help you to take advantage of the real power that is behind web optimization techniques, and give you a good grounding in using it in your websites.The book explores topics like HTML standards used for optimization, minifying scripts, and taking care of images, and solves the common errors that users do unknowingly. It will take you through a number of clear, practical recipes that will help you to take advantage of all the possible technologies to make your websitess efficient, fast, and accurate. You will not only learn basic standards to optimize your websites, but you will also learn some advanced level methods such as Apache handling, Flush methods, making AJAX cacheable, setting up browser caches, and reducing image size using CSS sprites. If you want to take advantage of all the necessary methods to make your website faster and efficient, then this book is a must-have.
Table of Contents (7 chapters)

Configuring ETags (Advanced )


Entity tags (ETags) is the way to determine that the contents available in the browser's cache is the same or different from the origin server.

ETags treats the CSS, JavaScript, and image files as entities, and provides a mechanism to validate the specific version of a component/entity that is returned from the server side under the response header as ETag: "some-value". Notice that it is stringified before sending the value to the client. Now, the server specifies the component's ETag using the ETag response header that changes it or has the same string value as of the previous one.

Now, if the browser has to check and validate the entity, it uses the If-None-Match request header to pass the ETag back to the server and in return the server responds with either 200 or 304 status code. If it returns 304 HTTP Code, the content has not been modified and saved some KBs to get downloaded.

How to do it...

  1. If it's the static content, it is easy to calculate the ETag by returning the recent modified date, so that if you update it and being requested by the browser; it would send HTTP code 200 and will send the updated one.

  2. But, if it's a dynamic file whose content changes frequently depending upon the argument and dynamic values, just sending the recent modified date would not be enough to calculate the ETag value. You may use PHP to generate ETag or any dynamic language would do that depending upon your requirement. Apache server automatically configures the ETag values.

  3. If all the files are going to be served from the same server, ETAG is important, else it should be disabled in case of CDN.

  4. Once can Set/Enable ETags using Apache's config file that is, httpd.conf or apache2.conf (depends upon which Apache version you are using) and add the following line at the end of the file:

    FileETag INode MTime Size

    Note

    Here, INode, MTimes, and Size mean:

    • INode: This shows the file's i-node number in the calculation

    • MTime: This shows the date and time the file was last modified on

    • Size: This shows the number of bytes of the file

  5. If you are using a cluster of servers, better disable it by adding the following lines in httpd.conf or apache2.conf at the end of this file:

    FileETag None
    Header unset ETag
  6. In case, it is misconfigured either by mistake of a programmer or web master, it would not show the If-None-Match tag under the request header. YSlow under Firefox shows that there are three files present in the site, which have misconfigured ETags.

How it works...

The process of requesting, configuring, and matching ETags can be explained with the step-by-step browser-server communication as follows:

  1. The user requests a web page, say abc.com, through the browser.

  2. The browser sends a request to the server to check and verify the freshness of the content.

  3. The server sends the output of a page of abc.com with the value of ETags as strings for the present page.

  4. The client gets the content and displays it after caching it with the ETags value for that page with the status code 200, if it's fresh content, as shown in the following screenshot:

  5. The client once again sends a request to the origin server by passing the previously sent ETags value through the If-None-Match tag.

  6. The server tests and verifies that the content has not been modified since the last time it was requested and sends a response as 304 status code with a 0 KB file size, as it is already in cache, as shown in the following screenshot:

There's more...

The concept of ETags may not work in the CDN services as it will get the ETag version from one server and the next time it would request to compare it with the others. There is little relief for you if you are using Apache, which is a major role player in the server domain, or IIS then by default, both embed data to the ETag header, which will reduce the chance of a failure on testing and verifying ETags even when coming from different servers.