Ghost/core/server/web/shared/middlewares/cache-control.js
Hannah Wolfe 37a22edbe9 Refactored cache-control mw to remove dependencies
- cache-control had some logic in it for private blogging + similar logic exists for members in site/app
- having it in 2 places is weird, and having it inside the mw makes the mw less generic/reusable
- instead of requiring config inside the middleware, we pass config in for the one case where this is used
- fixed tests that didn't test anything 🙈
2020-04-22 18:01:01 +01:00

32 lines
875 B
JavaScript

// # CacheControl Middleware
// Usage: cacheControl(profile), where profile is one of 'public' or 'private'
// After: checkIsPrivate
// Before: routes
// App: Admin|Site|API
//
// Allows each app to declare its own default caching rules
const isString = require('lodash/isString');
const cacheControl = (profile, options = {maxAge: 0}) => {
const profiles = {
public: `public, max-age=${options.maxAge}`,
private: 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
};
let output;
if (isString(profile) && Object.prototype.hasOwnProperty.call(profiles, profile)) {
output = profiles[profile];
}
return function cacheControlHeaders(req, res, next) {
if (output) {
res.set({'Cache-Control': output});
}
next();
};
};
module.exports = cacheControl;