[http] How does "304 Not Modified" work exactly?

  • How are "304 Not Modified" responses generated?

  • How does a browser determine whether the response to an HTTP request is 304?

  • Is it set by the browser or sent from the server?

  • If sent by the server, how does the server know the data available in cache, also how does it set 304 to an image?

My guess, if it's generated by the browser:

function is_modified()
{
    return get_data_from_cache() === get_data_from_url();
}

function get_data_from_cache()
{
    return some_hash_or_xxx_function(cache_data);
}

function get_data_from_url()
{
     return some_hash_or_xxx_function(new_data);
}

function some_hash_or_xxx_function(data)
{
     // Do something with the data.
     // What is that algorithm?
     return result;
}

console.log(is_modified());

I am relying on a third party API provider to get data, parse & push it to my database. The data may or may not change during every request, but the header always sends 200. I do not want to parse, check the last Unique ID in DB and so on... to determine the change in data, nor compare the result directly rather I md5(), sha1() and crc32() hashed the result and works fine, but I'm wondering about the algorithm to determine 304.

I want to use the same kind of algorithm to determine the change in my data.

This question is related to http browser http-headers http-status-code-304

The answer is


Last-Modified : The last modified date for the requested object

If-Modified-Since : Allows a 304 Not Modified to be returned if last modified date is unchanged.

ETag : An ETag is an opaque identifier assigned by a web server to a specific version of a resource found at a URL. If the resource representation at that URL ever changes, a new and different ETag is assigned.

If-None-Match : Allows a 304 Not Modified to be returned if ETag is unchanged.

the browser store cache with a date(Last-Modified) or id(ETag), when you need to request the URL again, the browser send request message with the header:

enter image description here

the server will return 304 when the if statement is False, and browser will use cache.


Examples related to http

Access blocked by CORS policy: Response to preflight request doesn't pass access control check Axios Delete request with body and headers? Read response headers from API response - Angular 5 + TypeScript Android 8: Cleartext HTTP traffic not permitted Angular 4 HttpClient Query Parameters Load json from local file with http.get() in angular 2 Angular 2: How to access an HTTP response body? What is HTTP "Host" header? Golang read request body Angular 2 - Checking for server errors from subscribe

Examples related to browser

How to force reloading a page when using browser back button? How do we download a blob url video How to prevent a browser from storing passwords How to Identify Microsoft Edge browser via CSS? Edit and replay XHR chrome/firefox etc? Communication between tabs or windows How do I render a Word document (.doc, .docx) in the browser using JavaScript? "Proxy server connection failed" in google chrome Chrome - ERR_CACHE_MISS How to check View Source in Mobile Browsers (Both Android && Feature Phone)

Examples related to http-headers

Set cookies for cross origin requests Adding a HTTP header to the Angular HttpClient doesn't send the header, why? Passing headers with axios POST request What is HTTP "Host" header? CORS error :Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response Using Axios GET with Authorization Header in React-Native App Axios get access to response header fields Custom header to HttpClient request Send multipart/form-data files with angular using $http Best HTTP Authorization header type for JWT

Examples related to http-status-code-304

How does "304 Not Modified" work exactly? NodeJS/express: Cache and 304 status code Why am I getting "(304) Not Modified" error on some links when using HttpWebRequest?