Details of the HTTP 304 error

  • 2020-03-29 23:40:23
  • OfStack

The Not Modified client has a cached document and makes a conditional request (typically providing an if-modified-since header to indicate that the client only wants the document to be updated by the specified date). The server tells the client that the previously cached document can still be used.

If the client finds that the cached file has a Last Modified when it requests a file, the request will contain If Modified Since, which is the Last Modified time of the cached file. Therefore, If Modified Since is included in the request, caching is already present on the client. Simply determine this time and the modification time of the currently requested file to determine whether to return 304 or 200. For static files, such as CSS and images, the server will automatically complete the comparison between Last Modified and If Modified Since, complete the cache or update. However, for dynamic pages, that is, dynamically generated pages, there is often no Last Modified information, so the browser, gateway, etc., will not do cache, that is, every request to complete a request of 200.

Therefore, for dynamic page cache acceleration, first add the Last Modified definition in the HTTP Header of Response, and then return 200 or 304 according to the If Modified Since in Request and the update time of the requested content. Although I have done a database query when I returned 304, I can avoid more database queries in the future, and return no page content but just an HTTP Header, thus greatly reducing the bandwidth consumption and improving the user's feeling.

When these caches are valid, viewing a request through HttpWatch results in:

First visit 200

Mouse click second access (Cache)

Press F5 to refresh 304

Press Ctrl+F5 to force a 200 refresh

If this is the case then the cache is actually valid. So that's my understanding of HTTP 304.

Related articles: