diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f5e00c..7399090 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ This major version has been reworked to avoid breaking changes but some edge-cas ## New Features -???? +### Cache-Control header + +Redis middleware now add a cache-control header with a max-age based on your route expiration, you can opt-out of this behavior by using `res.express_redis_cache_disable_cache_control=true` # Changes between 0.0.8 and 0.1.x diff --git a/README.md b/README.md index 53512c6..6b117c2 100644 --- a/README.md +++ b/README.md @@ -215,7 +215,10 @@ app.get('/index.html', function (req, res) { ... }); ``` -You can also specify +## Cache-Control header + +Redis middleware add a cache-control header with a max-age based on your route expiration, +you can opt-out of this behavior by using `res.express_redis_cache_disable_cache_control=true` # Content Type diff --git a/lib/ExpressRedisCache/route.js b/lib/ExpressRedisCache/route.js index c4fdced..f1fc16d 100644 --- a/lib/ExpressRedisCache/route.js +++ b/lib/ExpressRedisCache/route.js @@ -162,12 +162,16 @@ function route() { return next(); } - if (!res._headers["cache-control"] && typeof expire === "number") { + /** set a default cache-control value based on the default expiration **/ + if ( + !res._headers["cache-control"] && + typeof expire === "number" && + !res.express_redis_cache_disable_cache_control + ) { res._headers["cache-control"] = `max-age=${expire}000`; } /** if it's cached, display cache **/ - if (cache.length && cache[0].body != null) { res.contentType(cache[0].type || "text/html"); if (binary) { @@ -195,13 +199,23 @@ function route() { return ret; } + const expire = expirationPolicy(req, res); + + /** update the default cache-control value based on the response status **/ + if ( + !res._headers["cache-control"] && + typeof expire === "number" && + !res.express_redis_cache_disable_cache_control + ) { + res._headers["cache-control"] = `max-age=${expire}000`; + } /** Create the new cache **/ self.add( name, body, { type: this._headers["content-type"], - expire: expirationPolicy(req, res), + expire, tag }, function (name, cache) {}); // eslint-disable-line