0

I am confused by the way Firefox shows loaded resources as cached but then I can see the browser issues new DNS query for the domain name and contact the HTTP server again.

Below is a simple example of this behavior. I visited https://example.com then reload the webpage. As expected, the loaded resources are shown as cached. But, at the same time, I run tcpdump in background and notice that the browser issues DNS query for example.com again and also connects to the HTTP server after getting the DNS response.

dd Is this an expected behavior or am I missing something? Thank you for reading my question <3

23r23f23q
  • 123
  • 6

1 Answers1

1

Firefox has cached your object and is revalidating the object with the origin server. You can tell this because it sent the If-Modified-Since: and Cache-Control: request headers.

Firefox will generally revalidate cached objects in two situations:

  1. The cached object is stale, as determined by the Cache-Control: and Expires: response headers that were sent with the object when it was previously cached.
  2. You hit Reload.

You can see that the web server sent a 304 Not Modified response, and thus Firefox served the existing cached object.


HTTP caching can get quite complex. To learn all about it, spend an evening studying RFC 7234.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • Thanks @MichaelHampton for the prompt answer. So does it mean that even when HTTP resources are cached, the browser will still need to communicate with the server to verify the `validity` of the cached resources? How prevalence is this across other web browsers? – 23r23f23q Jul 30 '20 at 00:22
  • 1
    @23r23f23q When the cached entry is stale or you specifically hit Reload. AFAIK all major browsers behave this way. – Michael Hampton Jul 30 '20 at 00:25
  • so do you know if there's a set of directives in the VERY FIRST HTTP response that can we use to predict the `revalidation` behavior of the web browser if the given resource is visited again? So far, based on the description you have above, can we always expect the browser to `revalidate` a cached object if its initial response contains any of the following directives: `etag`, `expires`, `last-modified`, etc...? – 23r23f23q Jul 30 '20 at 01:59
  • 1
    @23r23f23q If the response is cacheable, then the browser will cache it until it is stale, and then it will revalidate. Unless of course you hit Reload, then it will revalidate everything. At this point you really ought to spend some time with the RFC. – Michael Hampton Jul 30 '20 at 02:12
  • ok, thank you so much for your help! Your answer and the pointer to the RFC are very helpful <3 – 23r23f23q Jul 30 '20 at 04:24