mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 18:31:57 +03:00
4f9b72ff43
- This is a minor bugbare, but it will affect some configuration I'm about to do for c8 - I've been wanting to do it for ages, middleware is plural all on it's own so it's an odd affectation in our codebase - This also only exists in 2 places, everywhere else we use "middleware" - Sadly it did result in a lot of churn as I did a full find and replace, but consistency is king!
32 lines
875 B
JavaScript
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;
|