Web Technology Basics
A browser requests an image it received earlier once again, putting the ETag value from that earlier response into the If-None-Match header. If the image on the server has not changed, which of the following is the correct server response?
- A200 OK, sending the whole image body again even though it is the same as before
- B304 Not Modified with no body, and the browser uses the image it has stored
- C404 Not Found, making the browser delete its stored old image and fetch it again
Show answer and explanation
Answer: B. 304 Not Modified with no body, and the browser uses the image it has stored
An ETag is an identifier that represents a particular version of a resource. The browser puts the ETag into If-None-Match to send a conditional request meaning "send it if it has changed since this version", and if it has not changed, the server returns 304 Not Modified without a body. This takes only the one round trip for the check, and the image data does not have to be sent again. If it has changed, the server returns 200 OK with the new body and a new ETag. Within the period set by max-age in Cache-Control, the browser uses its stored copy without even this check.
- A. The image has not changed, so the body need not be sent again. Avoiding that is the purpose of a conditional request.
- B. Correct. 304 means "it has not changed, so the stored copy may be used".
- C. 404 is the code for a resource that was not found, but the image exists.