Preface
If you are a web developer or website administrator/owner, sooner or later you will surely require the techniques to optimize your website by using various available resources, by tweaking server settings, and so on. This book will show you how to optimize the speed of your website through various common techniques.
What this book covers
Minimizing HTTP requests (Simple) shows you that lesser the number of requests, faster the website will be. This recipe will show you how to minimize such HTTP requests without hampering the website.
Adding an Expires or Cache-Control header (Simple) shows us that adding the Expires or Cache-Control header will make downloadable files cacheable and will save time in opening websites without any delay.
Following HTML standards (Simple) teaches us that by following proper HTML standards, we can reduce the number of warning and errors that browsers catch and make the website faster by using correct tags, elements, and so on.
Reducing DOM elements (Intermediate) demonstrates that the website will take less time if there are lesser DOM elements contained within it. We can reduce it significantly by removing unnecessary DOM elements through close observation.
Setting up CSS sprites (Intermediate) shows us that by using CSS codes to show images wherever required, we can save time and space. Using one large image is better than using several different images. It also shows us how to use the CSS sprites technique on it.
Enabling compression (Simple) shows us that compressing the file size over the network would reduce the time taken to send an HTTP request and get an HTTP response back.
Optimizing images (Simple) urges us to use an image format wisely. This recipe will deal with various ways to optimize an image for the site to run faster.
Minifying JavaScript and CSS files (Simple) introduces the concept of minification, which is the technique that one should use in the production of a website in order to reduce the size of the site and thus increasing its speed.
Setting up browser caching (Simple) explains that the browser cache depends more on the user than the website owner, and the Cache-Control header can be used for this purpose.
Using Apache mod_pagespeed – installing and configuring (Advanced) introduces Apache's mod_pagespeed module, which takes care of web optimization's best practices automatically after setting up and configuring properly. We will show you how to achieve it using practical examples and code.
Making favicon.ico small and cacheable (Simple) has less impact on website optimization, but it's a good habit to make favicon simple, small, and cacheable.
Avoiding redirects and Not Found errors – 301 and 404 codes (Advanced) shows that broken links, redirections, and bad requests are never good for a website's health. So, it's better that you try to minimize it.
Using the Flush Method (Intermediate) introduces flushing, which is beneficial for those websites where a lot of backend requests are sent or the frontend bears less content to display. In this recipe, we will show how to use it efficiently in your website.
Configuring ETags (Advanced) provides a mechanism to validate the specific version of a component/entity that is returned from the server side under the response header as an ETag.
Making AJAX cacheable (Intermediate) explains how we can make AJAX cacheable by adding the Expires header to the file that will be called by AJAX; for example, autoform completion.
Rules for using the CSS, JavaScript, and image files (Simple) shows the many other ways to enhance the web response, which don't fit in any of the preceding chapters appropriately.
What you need for this book
You need the following things to perform all the recipes provided in this book:
- A web browser (Firefox preferred)
- A system with minimal configuration (Windows/Linux preferred)
- Basic web development skills
- Familiarity with HTML, CSS, and TCP/IP protocols
- A text editor (Notepad++ preferred)
- Basic knowledge of networking protocols
Who this book is for
This book is for anyone who wants to know how to optimize their website to get more traffic, to reduce the size, to minimize warnings/errors, and so on. This book can prove useful to website administrators, owners, and developers who have full access to their websites to do all the listed experiments with full permission.
Conventions
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text are shown as follows: "We can do it by using area
and map
tags to make it work like the previous one."
A block of code is set as follows:
<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') ob_start("ob_gzhandler"); else ob_start(); ?>
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
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
Any command-line input or output is written as follows:
alert(document.getElementsByTagName('*').length);
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Run the following command on Firebug's console under the Script tab on whichever website you wish to check the total number of DOM elements."
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <feedback@packtpub.com>
, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <copyright@packtpub.com>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
Questions
You can contact us at <questions@packtpub.com>
if you are having a problem with any aspect of the book, and we will do our best to address it.