【发布时间】:2021-06-11 21:58:30
【问题描述】:
我正在使用带有 node/express 的 create react 应用程序。我正在尝试解决 Lighthouse 报告的性能问题“使用有效的缓存策略提供静态资产”。
我尝试过以多种方式设置 Cache-Control。以下是当前配置,但它不起作用。
let options = {
etag: true,
maxAge: 31536000,
redirect: true,
setHeaders: function (res, path, stat) {
res.set(
{
'x-timestamp': Date.now(),
'robert': 'hi'
}
)
}
};
let indexOptions = {
'Cache-Control': 'no-cache'
};
if (process.env.NODE_ENV === 'production') {
// Serve any static files
// app.use(express.static(path.join(__dirname, 'client/build')));
// ADDED OPTIONS WITH CACHE EXPIRY
app.use(express.static(path.join(__dirname, 'client/build'), options));
// app.use(express.static(path.join(__dirname, 'client/build'), {'Cache-Control': 'client/build, max-age=31536000'}));
// Handle React routing, return all requests to React app
app.get('*', function (req, res) {
// res.sendFile(path.join(__dirname, 'client/build', 'index.html'), {'Cache-Control': 'no-cache'});
// res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
res.sendFile(path.join(__dirname, 'client/build', 'index.html'), indexOptions);
});
}
我没有收到任何错误,只是没有在索引或其他文件上设置任何错误。
【问题讨论】:
标签: node.js webpack create-react-app cache-control