2015-05-18 19:12:42 +01:00
|
|
|
// # CacheControl Middleware
|
|
|
|
// Usage: cacheControl(profile), where profile is one of 'public' or 'private'
|
|
|
|
// After: checkIsPrivate
|
|
|
|
// Before: routes
|
2017-10-26 17:24:08 +01:00
|
|
|
// App: Admin|Site|API
|
2015-05-18 19:12:42 +01:00
|
|
|
//
|
|
|
|
// Allows each app to declare its own default caching rules
|
|
|
|
|
2018-09-20 15:03:33 +02:00
|
|
|
const isString = require('lodash/isString');
|
2015-05-18 19:12:42 +01:00
|
|
|
|
2020-04-22 17:27:43 +01:00
|
|
|
const cacheControl = (profile, options = {maxAge: 0}) => {
|
2018-09-10 08:07:57 -04:00
|
|
|
const profiles = {
|
2020-04-22 17:27:43 +01:00
|
|
|
public: `public, max-age=${options.maxAge}`,
|
2018-09-10 08:07:57 -04:00
|
|
|
private: 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
|
|
|
};
|
|
|
|
|
|
|
|
let output;
|
2015-05-18 19:12:42 +01:00
|
|
|
|
2020-04-22 17:27:43 +01:00
|
|
|
if (isString(profile) && Object.prototype.hasOwnProperty.call(profiles, profile)) {
|
|
|
|
output = profiles[profile];
|
2015-05-18 19:12:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return function cacheControlHeaders(req, res, next) {
|
|
|
|
if (output) {
|
2020-04-22 17:27:43 +01:00
|
|
|
res.set({'Cache-Control': output});
|
2015-05-18 19:12:42 +01:00
|
|
|
}
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = cacheControl;
|