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)

Adding an Expires or Cache-Control header (Simple)


Websites usually contain the mixture of static and dynamic elements that are being used uniformly over different pages. When a browser starts loading a web page, it usually downloads all the images, CSS files, JavaScript files, and Flash files, if any. Imagine if every time the browser had to download each and every file associated with that page, how hectic it would be for both the browser and the user, and you would have a bandwidth problem too. Gone are the days when people used few static pages with less creativity. Now, almost all websites contain a lot of JavaScript and CSS scripts; images that make the site a bit slower and heavy.

Getting ready

We can load a web page faster if we cache the images and scripts used on the website into the user's browser cache. It will only help in optimizing the website if it has been already visited and the cache is not empty, else it has no effect. The following screenshot shows which static files are required to have far future expiry dates that are cacheable:

How to do it...

By using the Expires headers, these components become cacheable, that is, these avoid unnecessary HTTP requests on subsequent page views. The Expires headers are most often associated with images, but they can and should be used on all page components including scripts, stylesheets, and Flash. We can do this in two ways. The first way is by using the Never Expires setting for all static components and setting the value to a far future date. If you are using a 32-bit system, better set the "expires date" till January 18, 2038, else it won't work on a 32-bit system without tweaking the code.

Add the Expires header by using the meta tag for static contents. Consider a site that requests the header with the following meta tag:

<META HTTP-EQUIV="Expires" CONTENT="Sat, 04 Dec 2021 21:29:02 GMT">

The response header would contain the Expires term like this:

Expires: Sat, 04 Dec 2021 21:29:02 GMT

Another way is by using Cache-Control metadata to assist the browser with the requests. For dynamic contents we use Cache-Control header definition like the following:

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="PUBLIC">

Content can be any of the four values: public, private, no-cache, or no-store. Here is some information regarding these four values:

  • public: The content set to public will be cached in public-shared caches

  • private: This content will only be cached in private cache

  • no-cache: This content will not be cached

  • no-store: This content will be cached but not archived

If you have Firebug enabled in your Firefox browser, you can see the Cache-Control header's value at the request and response header as defined in RFC 2616 (http://www.ietf.org/rfc/rfc2616.txt).

The following are the two cache-related header parts that you should know:

cache-request-directive = "no-cache"  | "no-store"  | "max-age" "=" delta-seconds  | "max-stale" [ "=" delta-seconds  ] | "min-fresh" "=" delta-seconds   | "no-transform"  | "only-if-cached" | cache-extension                     
cache-response-directive ="public"  | "private" [ "=" <"> 1#field-name <"> ] | "no-cache" [ "=" <"> 1#field-name <"> ] | "no-store"   | "no-transform"  | "must-revalidate"  | "proxy-revalidate" | "max-age" "=" delta-seconds  | "s-maxage" "=" delta-seconds     

Note

For more information on the Cache-Control general HTML header field, please go through W3C's Header Field Definitions (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html) under section 14.9.

How it works...

So, when you open any website for the first time it would return status 200 and will take some time to load the page because it will download each bit of the page. It all depends on your browser settings and cookies handler options.

When you would visit the same site next time, the latency period would be much lesser and it would return the 304 status code for the scripts or files, as shown in the following screenshot:

When the status is 200, the response header would show how much data is downloaded for a particular request. In the following screenshot, it shows 21501 bytes under the Content-Length attribute:

If the status code is 304, you will observe no data has been downloaded, as shown in the following screenshot:

Moreover, check the Expires section in the previous screenshot to get an idea of caching here.

There's more...

This Cache-Control header technique and the meta tag bearing Expires were introduced from HTTP/1.1 onward.

HTTP 1.1 (RFC 2068), available at http://www.w3.org/Protocols/rfc2068/rfc2068, specifies that all HTTP date/time stamps must be generated in Greenwich Mean Time (GMT) and the format should follow the rules defined in RFC 1123, that is, weekday "," date time "GMT".

  • weekday would bear values including Mon, Tue, Wed, Thu, Fri, Sat, or Sun.

  • date should be written in this format: day(2 digits) month(3 chars) year(4 digits). For example, 02 Jun 1982.

    The month should be represented using Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, or Dec).

  • time should be formatted as Hour ":" Minutes":" Seconds, which ranges from 00:00:00 to 23:59:59.

So, an example of this would be, CONTENT="Sat, 04 Dec 2021 21:29:02 GMT".