这基本上是相反的fetch(),如何发出非缓存请求? https://stackoverflow.com/questions/29246444/fetch-how-do-you-make-a-non-cached-request.
假设我们有客户端:
<div onclick="fetch('/data.php').then(r => r.text()).then(s => console.log(s));">Click me</div>
PHP 中的服务器端:
<?php echo "hello"; /* in reality, this can be much more data
and/or updated later with other content */ ?>
点击时多次 on Click me
,请求通过网络多次完成,请求响应代码为“200”。
Thus, 没有进行缓存通过浏览器/通过 PHP:
如果实际上不需要这些数据,如何防止多次传输?
潜在的解决方案:自/data.php
可以在服务器上更新,服务器可以使用日期时间元数据来服务数据请求timestamp
,我可以将其存储在客户端 JS 变量中timestamp
。然后在进行下一步之前fetch
要求/data.php
,我可以提出初步要求
fetch('/get_last_modification_date.php').(r => r.json()).then(data => {
if (data['timestamp'] != timestamp)
fetch('/data.php')... // if and only if new data is here
});
因此,我们only do a fetch('/data.php')...
如果该数据的时间戳比我们已有的时间戳更新。
这可行,但似乎我们正在手动做一些可以通过浏览器+PHP缓存机制完成的事情。
如何要求 PHP 和浏览器为我们完成所有这些缓存?