节点,特定文件类型的不同缓存规则

时间:2017-04-07 10:13:52

标签: node.js cache-control

我正在为我的应用程序禁用缓存,如下所示:

app.use(function noCache(req, res, next) {
  res.header('Cache-Control', 'no-cache, no-store, must-revalidate');
  res.header('Pragma', 'no-cache');
  res.header('Expires', 0);
  next();
});

使用IE时,使用此功能时出现问题:https://connect.microsoft.com/IE/feedbackdetail/view/992569/font-face-not-working-with-internet-explorer-and-http-header-pragma-no-cache

如何更改上述代码使其不适用于字体类型文件?我认为这将解决我的问题。

由于

1 个答案:

答案 0 :(得分:1)

您可以针对网络字体(ttf,woff,eot等)的扩展程序检查req.path,并在这种情况下跳过发回这些标题:

const WEBFONT_EXTENSIONS = /\.(?:eot|ttf|woff|svg)$/i;

app.use(function noCache(req, res, next) {
  if (! WEBFONT_EXTENSIONS.test(req.path)) {
    res.header('Cache-Control', 'no-cache, no-store, must-revalidate');
    res.header('Pragma', 'no-cache');
    res.header('Expires', 0);
  }
  next();
});
相关问题