req.fresh returns true when the client's cached copy of a resource is still valid,
based on cache-related request and response headers; otherwise it returns false.
Syntax
req.fresh
Example
const express = require("express");
const app = express();
const PORT = 4700;
app.get("/article", (req, res) => {
res.set("ETag", '"article-v1"');
if (req.fresh) {
return res.status(304).end();
}
res.send("Full article content");
});
app.listen(PORT, () => console.log(`Listening on ${PORT}`));
The first request returns the article body. A follow-up request that includes
If-None-Match: "article-v1" is considered fresh, and the server replies with 304 instead
of resending the body.
Use cases
- Skipping unnecessary re-transfers of unchanged content.
- Implementing conditional GET requests.
- Supporting
ETag/Last-Modified-based caching strategies.