“304未修改"如何处理工作准确吗? [英] How does "304 Not Modified" work exactly?

查看:33
本文介绍了“304未修改"如何处理工作准确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 304 Not Modified"响应是如何生成的?

  • How are "304 Not Modified" responses generated?

浏览器如何判断对 HTTP 请求的响应是否为 304?

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?

如果由服务器发送,服务器如何知道缓存中可用的数据,以及如何将304设置为图像?

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

我的猜测,如果它是由浏览器生成的:

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());

我依赖第三方 API 提供者来获取数据、解析 &将其推送到我的数据库.数据在每次请求期间可能会也可能不会更改,但标头始终发送 200.我不想解析,检查DB中的最后一个Unique ID等等......来确定数据的变化,也不想直接比较结果而不是我md5(), sha1()crc32() 对结果进行哈希处理并且工作正常,但我想知道确定 304 的算法.

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.

推荐答案

当浏览器将某些内容放入其缓存时,它还会存储 Last-ModifiedETag 标头来自服务器.

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

然后浏览器发送带有 If-Modified-SinceIf-None-Match 标头的请求,告诉服务器如果内容仍然存在,则发送 304那个日期或 ETag.

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.

服务器需要某种方式为每个资源的每个版本计算修改日期或 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.

这篇关于“304未修改"如何处理工作准确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆