在请求标题中设置缓存控制
根据 this
“ max-age”请求指令表示客户不需要 接受年龄大于指定数量的回应 秒
,所以我尝试设置一个小应用程序来测试它。我在请求中设置缓存控制:'max-age = 5'
,cache-control:'max-age = 10'
在响应中。如果我正确理解上述句子,那么我5秒钟后我提出的每个请求都应该收到全新的回应。但是5秒钟后,我仍然收到“旧响应”。我错过了什么吗?
这是代码:
客户:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="onClick()">
Fetch
</button>
<div class="response-container">
</div>
<script>
const onClick = async () => {
const response = await fetch('http://localhost:3000/hello', {
headers: {
'Cache-Control': 'max-age=5'
}
});
const result = await response.json();
console.log('result', result)
// create a div and append to div with class .response-container
const div = document.createElement('div');
div.innerHTML = result.message;
document.querySelector('.response-container').appendChild(div);
}
</script>
</body>
</html>
Server:
var express = require("express");
var app = express();
const cors = require("cors");
app.use(cors());
let requestIdx = 1;
app.get("/hello", function (req, res) {
// set cache control headers to expire in 10 seconds
res.setHeader("Cache-Control", "max-age=10");
res.send({
message: "Hello World" + requestIdx++,
});
});
app.listen(3000, function () {
console.log("Server started on port 3000");
});
According to this
The "max-age" request directive indicates that the client is unwilling
to accept a response whose age is greater than the specified number of
seconds
So I tried setting up a small app to test it out. I set the Cache-Control: 'max-age=5'
in the request and Cache-Control: 'max-age=10'
in the response. If I understand the above sentence correctly, every request I make after 5 seconds should receive a brand new response. But after 5 seconds, I still receive the "old response". Am I missing anything?
Here are code:
Client:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="onClick()">
Fetch
</button>
<div class="response-container">
</div>
<script>
const onClick = async () => {
const response = await fetch('http://localhost:3000/hello', {
headers: {
'Cache-Control': 'max-age=5'
}
});
const result = await response.json();
console.log('result', result)
// create a div and append to div with class .response-container
const div = document.createElement('div');
div.innerHTML = result.message;
document.querySelector('.response-container').appendChild(div);
}
</script>
</body>
</html>
Server:
var express = require("express");
var app = express();
const cors = require("cors");
app.use(cors());
let requestIdx = 1;
app.get("/hello", function (req, res) {
// set cache control headers to expire in 10 seconds
res.setHeader("Cache-Control", "max-age=10");
res.send({
message: "Hello World" + requestIdx++,
});
});
app.listen(3000, function () {
console.log("Server started on port 3000");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,根据您的问题,我猜您从服务器传递的任何内容都可以起作用。如果要进行缓存控制,客户端无需完成。它应该始终作为响应标头的一部分而不是请求标题。
So, as per your question I guess whatever you've passed from server it would work. In case of cache control there's nothing needs to be done from client. It should always come as part of response header not request header.