Skip to content Skip to sidebar Skip to footer

Why Does JavaScript's Fetch() Not Need To Check For Status Code 304?

The code to show how to use fetch() properly, even to handle 404 or 500 which is considered not an error, is to check for response.ok: fetch(' ') .then(response => { if

Solution 1:

You are doing an http-fetch request, which at step 3 enters the http-network-or-cache-fetch algorithm, which itself would enter the step 8.23 because the request is probably already in your cache.

  1. Set response to storedResponse.

Then the algorithm will start a new revalidateRequest to revalidate the cache in parallel, which will have its response's status set to 304, but the one response that is being returned is actually the cached one, not that 304 response.

As the note in the specs states:

This fetch is only meant to update the state of httpCache and the response will be unused until another cache access. The stale response will be used as the response to current request.

The storedResponse that is being returned was probably not a 304 response, and certainly not a null body response, so you don't need to check for it.

If the server did return a 304 response with null body, then you'd fall in the step 10 which would return a NetworkError.

Here is a glitch setup showing this, somehow.


Post a Comment for "Why Does JavaScript's Fetch() Not Need To Check For Status Code 304?"