NodeJS:如何防止发送模块缓存控制覆盖 HTeAccess 缓存


<FilesMatch ""> 
Header set Cache-Control "no-cache, no-store, must-revalidate"
</FilesMatch>
<FilesMatch ".*"> 
Header set Cache-Control "max-age=604800, s-maxage=604800, public, must-revalidate"
</FilesMatch>
<FilesMatch ".(js)">
Header set Cache-Control "max-age=604800, s-maxage=604800, private, must-revalidate"
</FilesMatch>

我已经使用htaccess将缓存添加到静态资产,如css,js,svgs。这些缓存设置不会反映在应用程序中,因为它被快速发送模块中的以下代码覆盖。

if (this._cacheControl && !res.getHeader('Cache-Control')) {
var cacheControl = 'public, max-age=' + Math.floor(this._maxage / 1000)
debug('cache-control %s', cacheControl)
res.setHeader('Cache-Control', cacheControl) // this line is responsible for that
}

当我在上面的 setHeader 代码中添加注释时,htaccess 缓存设置将缓存正确设置为资产。但是护照认证失败。就像在注释掉 setHeader 行时未设置身份验证会话一样。 那么如何防止覆盖htaccess设置的缓存呢?

可以通过将cacheControl选项设置为false来禁用Cache-Control

缓存控制

启用或禁用Cache-Control响应标头设置,默认为true。 禁用此选项将忽略immutablemaxAge选项。

app.use('/', express.static('./', {
cacheControl :false
}));

express.static将选项传递到用于发送文件的发送包。

最新更新