Speed is essential for all websites, but especially for sites where users spend much time and navigate through many pages. Reducing the traffic will improve the user’s experience and lower your traffic costs.
Your web server is already using eTags and Last-Modified headers to help browsers validate the items in their cache. This ensures you don’t transfer data twice (as long as it is cached), and the cache is immediately updated when an item changes on your web server. These techniques already do a reasonably good job for you.
However, these still require a browser to make a call to the server to validate each item. Although this is a minimal request, it still requires server resources, bandwidth, and most essentially: time. With many items, as any non-trivial website or webapp will have, it adds up and affects your performance and your users.
I’ll explain an alternative strategy for your most common resources which will help you speed up your site. Using this will help you make the most of existing internet architecture, such as browser caches, proxy servers, front-end caches and content delivery networks. It is not a solution for every file on your website, but you should use this for all content that is used across many pages of your site and changes infrequently.
Today’s solution
Most web developers will store their documents in a structure that looks somewhat like this today:
- /index.htm
- /css/common.css
- /js/common.js
- /gfx/sitelogo.png
- /gfx/background.png
- … etc …
When you make a change to e.g. sitelogo.png, you update that file, and voila - all your users see that change. Also, each time a page is loaded, their web browsers will tell your server what version of sitelogo.png it has, and download the data if changed.
Your next solution
By using the Cache-Control or Expires headers, you can instruct your clients to keep an item in the cache for a certain time period. It is easiest to use the Cache-Control header in most web servers. The header below allows clients to cache an item for 30 days:
- Cache-Control: public; max-age: 2592000
Create a folder, e.g. /commonres, and ask your server administrator to enable the header above for this directory structure.
Then, create a version folder below this each time you push an update to your server. The folder structure now looks something like this:
- /index.htm
- /commonres/1.0/css/common.css
- /commonres/1.0/js/common.js
- /commonres/1.0/gfx/sitelogo.png
You can now push new files into this structure, and they will be correctly downloaded by clients. However, if you change any of these files you must rename the version part of the path and update all your references to point to this new location.
When making major changes to your site, copy the folder 1.0 to 1.1, and push to your website:
- /index.htm
- /commonres/1.1/css/common.css
- /commonres/1.1/js/common.js
- /commonres/1.1/gfx/sitelogo.png
Automating the updates
Obviously, if you have many manually crafted pages on your site, using Search/Replace to update all the version numbers can become tedious.
I therefore strongly recommend that you have some kind of global variable which is set by you in one place, and then use this variable to create the resource URLs. This will work in absolutely all scripting languages.
Wrapping up
This technique requires you to think about versioning of your site, but will make a big impact on performance in the long run.
Another benefit is that by thinking about which content you put where, you are well prepared for introducing a content delivery network in the future.