How does "304 Not Modified" work exactly?
Asked Answered
H

2

194
  • 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.

Horst answered 7/1, 2014 at 17:36 Comment(2)
Yes I googled for how 304 not modified works, but did not get any answer.Horst
You need to be a bit more general. google.com/search?q=http%20cachingBedplate
B
221

When the browser puts something in its cache, it also stores the Last-Modified or ETag header from the server.

The browser then sends a request with the If-Modified-Since or If-None-Match header, telling the server to send a 304 if the content still has that date or ETag.

The server needs some way of calculating a date-modified or ETag for each version of each resource; this typically comes from the filesystem or a separate database column.

Bedplate answered 7/1, 2014 at 17:41 Comment(7)
ETag is the keyword, checked with headers ETag remains same in both Response Headers & Response Headers From Cache, can you tell the algorithm behind ETag. I have updated my question stating my requirement.Horst
@VenomVendor: ETag is just a field where the server can store a unique ID (typically a hash or version number or vector clock). It doesn't help you calculate that ID at all; that's up to your server-side code.Bedplate
@SLaks: What happens if the page has a db call...There is a chance that the data in the db would have changed..In this case it does not make sense to check for the last-modified call, Correct?..How does it this condition is looked into?Biondo
@user1050619: It is up to your server to make sure that the ETag is accurate. If you show data from a DB, you need to include that.Bedplate
One thing that's still unclear is whether if you have a large max-age then does the browser need to make the request? (as it could stub in 304 and not make the request at all)... you want this e.g. with "fingerprinted" assets (they're good forever). Else what's the point of max-age...Junno
This answer isn't complete. Sometimes a response contains neither Last-Modified nor ETAG headers yet can still respond with a 304.Effervesce
@Octopus: A 304 response doesn't need those headers, since it's already cached.Bedplate
B
24

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.

Barkley answered 6/3, 2017 at 2:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.