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