简体   繁体   中英

Node.js express force client to skip cache (refresh)

I'm sending a file in node.js / express like this:

res.header("Content-Type", mime.lookup(file));
res.sendFile(file);

On sever side I can detect if that file has changed and I want to force client to skip cache only in that case.

something like:

res.header("Content-Type", mime.lookup(file));
if (fileHasChanged(file))
  res.header("some-header-telling-client-to-skip-cache", "some-value");
res.sendFile(file);

How can I do that?

Looks like, you need to add 'Cache-Control': 'no-cache' to you the sendFile() options:

const options = {
  headers: {
    'Cache-Control': 'no-cache',
  }
};

res.sendFile(file, options, (err) => {
  if (err) {
    next(err);
  } else {
    // Success
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM