Skip to content

Commit

Permalink
馃帹 add cache control configurations into the default config
Browse files Browse the repository at this point in the history
refs #7488

- cache control can be overridden if needed
  • Loading branch information
kirrg001 authored and kevinansfield committed May 31, 2017
1 parent e3ef310 commit 78ac63d
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
4 changes: 3 additions & 1 deletion core/server/apps/private-blogging/lib/middleware.js
Expand Up @@ -60,11 +60,13 @@ privateBlogging = {
if (err) {
return next(err);
}

res.writeHead(200, {
'Content-Type': 'text/plain',
'Content-Length': buf.length,
'Cache-Control': 'public, max-age=' + utils.ONE_HOUR_MS
'Cache-Control': 'public, max-age=' + config.get('caching:robotstxt:maxAge')
});

res.end(buf);
});
} else {
Expand Down
20 changes: 20 additions & 0 deletions core/server/config/defaults.json
Expand Up @@ -53,5 +53,25 @@
"lifetime": 3600,
"freeRetries":99
}
},
"caching": {
"frontend": {
"maxAge": 0
},
"301": {
"maxAge": 31536000
},
"customRedirects": {
"maxAge": 31536000
},
"favicon": {
"maxAge": 86400
},
"sitemap": {
"maxAge": 3600
},
"robotstxt": {
"maxAge": 3600000
}
}
}
6 changes: 3 additions & 3 deletions core/server/data/xml/sitemap/handler.js
@@ -1,5 +1,5 @@
var _ = require('lodash'),
utils = require('../../../utils'),
config = require('../../../config'),
sitemap = require('./index');

// Responsible for handling requests for sitemap files
Expand All @@ -20,7 +20,7 @@ module.exports = function handler(blogApp) {
var siteMapXml = sitemap.getIndexXml();

res.set({
'Cache-Control': 'public, max-age=' + utils.ONE_HOUR_S,
'Cache-Control': 'public, max-age=' + config.get('caching:sitemap:maxAge'),
'Content-Type': 'text/xml'
});

Expand All @@ -45,7 +45,7 @@ module.exports = function handler(blogApp) {
siteMapXml = getResourceSiteMapXml(type, page);

res.set({
'Cache-Control': 'public, max-age=' + utils.ONE_HOUR_S,
'Cache-Control': 'public, max-age=' + config.get('caching:sitemap:maxAge'),
'Content-Type': 'text/xml'
});

Expand Down
3 changes: 2 additions & 1 deletion core/server/middleware/cache-control.js
Expand Up @@ -7,12 +7,13 @@
// Allows each app to declare its own default caching rules

var _ = require('lodash'),
config = require('../config'),
cacheControl;

cacheControl = function cacheControl(options) {
/*jslint unparam:true*/
var profiles = {
public: 'public, max-age=0',
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'
},
output;
Expand Down
5 changes: 2 additions & 3 deletions core/server/middleware/custom-redirects.js
Expand Up @@ -2,8 +2,7 @@ var fs = require('fs-extra'),
_ = require('lodash'),
config = require('../config'),
errors = require('../errors'),
logging = require('../logging'),
utils = require('../utils');
logging = require('../logging');

/**
* you can extend Ghost with a custom redirects file
Expand Down Expand Up @@ -42,7 +41,7 @@ module.exports = function redirects(blogApp) {
}

blogApp.get(new RegExp(redirect.from), function (req, res) {
var maxAge = redirect.permanent ? utils.ONE_YEAR_S : 0;
var maxAge = redirect.permanent ? config.get('caching:customRedirects:maxAge') : 0;

res.set({
'Cache-Control': 'public, max-age=' + maxAge
Expand Down
3 changes: 2 additions & 1 deletion core/server/middleware/serve-favicon.js
Expand Up @@ -3,6 +3,7 @@ var fs = require('fs'),
crypto = require('crypto'),
storage = require('../adapters/storage'),
utils = require('../utils'),
config = require('../config'),
settingsCache = require('../settings/cache'),
blogIconUtils = require('../utils/blog-icon'),
buildContentResponse,
Expand All @@ -14,7 +15,7 @@ buildContentResponse = function buildContentResponse(ext, buf) {
'Content-Type': 'image/' + ext,
'Content-Length': buf.length,
ETag: '"' + crypto.createHash('md5').update(buf, 'utf8').digest('hex') + '"',
'Cache-Control': 'public, max-age=' + utils.ONE_DAY_S
'Cache-Control': 'public, max-age=' + config.get('caching:favicon:maxAge')
},
body: buf
};
Expand Down
7 changes: 6 additions & 1 deletion core/server/utils/index.js
@@ -1,5 +1,6 @@
var unidecode = require('unidecode'),
_ = require('lodash'),
config = require('../config'),
utils,
getRandomInt;

Expand Down Expand Up @@ -54,6 +55,7 @@ utils = {

return buf.join('');
},

safeString: function (string, options) {
options = options || {};

Expand Down Expand Up @@ -89,11 +91,13 @@ utils = {

return string;
},

// The token is encoded URL safe by replacing '+' with '-', '\' with '_' and removing '='
// NOTE: the token is not encoded using valid base64 anymore
encodeBase64URLsafe: function (base64String) {
return base64String.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
},

// Decode url safe base64 encoding and add padding ('=')
decodeBase64URLsafe: function (base64String) {
base64String = base64String.replace(/-/g, '+').replace(/_/g, '/');
Expand All @@ -102,9 +106,10 @@ utils = {
}
return base64String;
},

redirect301: function redirect301(res, path) {
/*jslint unparam:true*/
res.set({'Cache-Control': 'public, max-age=' + utils.ONE_YEAR_S});
res.set({'Cache-Control': 'public, max-age=' + config.get('caching:301:maxAge')});
res.redirect(301, path);
},

Expand Down

0 comments on commit 78ac63d

Please sign in to comment.