Search code examples
node.jscache-control

Node, different cache rules for a particular file type


I am disabling cache for my app like so:

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();
});

There is a problem using this over HTTPS when using IE: https://connect.microsoft.com/IE/feedbackdetail/view/992569/font-face-not-working-with-internet-explorer-and-http-header-pragma-no-cache

How can I change the above code to make it not apply to font type files? I think this will solve my issue.

Thanks


Solution

  • You can check req.path against extensions for web fonts (ttf, woff, eot, etc), and skip sending back those headers in that case:

    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();
    });