Using ETags

With an Etag (or Entity Tag) it is possible to check with the server if a resource has changed the last time it is received. The server sends an Etag in the headers of the request when a resource is received. A md5 sum of the content is a good candidate for a Etag because it changes when the content changes. It could be useful when the resource is not static but it will not change very often. The controller in a web application can perform the ETag comparison and decide when to send a the resource or a not modified.

The first time a resource is accessed the ETag is added to the response headers.

HTTP/1.1 200 OK
ETag: 509eabd3221

When a second request is made the browser sends the following header with the request. In essence it aks the server to sent the resource if the current version of the resource is different from the previously retrieved version.

GET /resource
If-None-Match: 509eabd3221

The server now checks if the new calculated ETag is different than the ETag the browser has send. When the newly calculated ETag is the same the resource has not changed and the server responds with a 304 Not modified.

HTTP/1.1 304 Not modified

When a new version of the resource is available the server will respond with a normal 200 OK and the resource itself.

HTTP/1.1 200 OK
...

With the ETag and If-None-Match you can prevent loading large files over and over again. The browser will get the locally stored copy when it receives a 304 Not modified and that is always faster than reading it from the server.