我正在尝试在 my website 上设置浏览器缓存没有任何成功 我的 server.js 文件中有我的代码
app.use(function(req, res, next){
res.set({
Expires: new Date(Date.now() + 2592000000).toUTCString()
});
next();
})
app.use(express.static(path.join(__dirname, '../build/public'),{maxAge:2592000000}));
怎么了?
最佳答案
问题在于 express.static 的 maxAge
选项使用Cache-Control header 。
maxAge: Set the max-age property of the Cache-Control header in milliseconds or a string in ms format.
在 Expires header 文档您可以找到以下内容:
If there is a
Cache-Control
header with the "max-age" or "s-maxage" directive in the response, theExpires
header is ignored
因此,一个可能的解决方案是仅使用 Cache-Control header 。接下来的片段在快速中间件中设置此 header :
app.use((req, res, next) => {
res.header('Cache-Control', 'max-age=2592000000');
next();
});
关于node.js - 浏览器缓存 Express Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44437977/