在请求标题中设置缓存控制

发布于 2025-01-21 13:47:00 字数 2142 浏览 0 评论 0原文

根据 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?

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌吟 2025-01-28 13:47:00

因此,根据您的问题,我猜您从服务器传递的任何内容都可以起作用。如果要进行缓存控制,客户端无需完成。它应该始终作为响应标头的一部分而不是请求标题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文