mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-27 04:43:12 +03:00
Merge pull request #794 from sebgie/issue#570
Add invalidate cache headers
This commit is contained in:
commit
57d83fe560
@ -81,7 +81,14 @@ posts = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return canThis(this.user).remove.post(args.id).then(function () {
|
return canThis(this.user).remove.post(args.id).then(function () {
|
||||||
return dataProvider.Post.destroy(args.id);
|
return when(posts.read({id : args.id})).then(function (result) {
|
||||||
|
return dataProvider.Post.destroy(args.id).then(function () {
|
||||||
|
var deletedObj = {};
|
||||||
|
deletedObj.id = result.attributes.id;
|
||||||
|
deletedObj.slug = result.attributes.slug;
|
||||||
|
return deletedObj;
|
||||||
|
});
|
||||||
|
});
|
||||||
}, function () {
|
}, function () {
|
||||||
return when.reject("You do not have permission to remove posts.");
|
return when.reject("You do not have permission to remove posts.");
|
||||||
});
|
});
|
||||||
@ -301,6 +308,33 @@ settings = {
|
|||||||
|
|
||||||
// ## Request Handlers
|
// ## Request Handlers
|
||||||
|
|
||||||
|
function invalidateCache(req, res, result) {
|
||||||
|
var parsedUrl = req._parsedUrl.pathname.replace(/\/$/, '').split('/'),
|
||||||
|
method = req.method,
|
||||||
|
endpoint = parsedUrl[3],
|
||||||
|
id = parsedUrl[4],
|
||||||
|
cacheInvalidate;
|
||||||
|
if (method === 'POST' || method === 'PUT' || method === 'DELETE') {
|
||||||
|
if (endpoint === 'settings' || endpoint === 'users') {
|
||||||
|
cacheInvalidate = "/*";
|
||||||
|
} else if (endpoint === 'posts') {
|
||||||
|
cacheInvalidate = "/, /page/*, /rss/, /rss/*";
|
||||||
|
if (id) {
|
||||||
|
if (result.toJSON) {
|
||||||
|
cacheInvalidate += ', /' + result.toJSON().slug;
|
||||||
|
} else {
|
||||||
|
cacheInvalidate += ', /' + result.slug;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cacheInvalidate) {
|
||||||
|
res.set({
|
||||||
|
"X-Cache-Invalidate": cacheInvalidate
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ### requestHandler
|
// ### requestHandler
|
||||||
// decorator for api functions which are called via an HTTP request
|
// decorator for api functions which are called via an HTTP request
|
||||||
// takes the API method and wraps it so that it gets data from the request and returns a sensible JSON response
|
// takes the API method and wraps it so that it gets data from the request and returns a sensible JSON response
|
||||||
@ -312,6 +346,7 @@ requestHandler = function (apiMethod) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return apiMethod.call(apiContext, options).then(function (result) {
|
return apiMethod.call(apiContext, options).then(function (result) {
|
||||||
|
invalidateCache(req, res, result);
|
||||||
res.json(result || {});
|
res.json(result || {});
|
||||||
}, function (error) {
|
}, function (error) {
|
||||||
error = {error: _.isString(error) ? error : (_.isObject(error) ? error.message : 'Unknown API Error')};
|
error = {error: _.isString(error) ? error : (_.isObject(error) ? error.message : 'Unknown API Error')};
|
||||||
|
@ -369,6 +369,9 @@ adminControllers = {
|
|||||||
|
|
||||||
return api.notifications.add(notification).then(function () {
|
return api.notifications.add(notification).then(function () {
|
||||||
delete req.session.user;
|
delete req.session.user;
|
||||||
|
res.set({
|
||||||
|
"X-Cache-Invalidate": "/*"
|
||||||
|
});
|
||||||
res.redirect('/ghost/signin/');
|
res.redirect('/ghost/signin/');
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -399,6 +402,9 @@ adminControllers = {
|
|||||||
|
|
||||||
return api.notifications.add(notification).then(function () {
|
return api.notifications.add(notification).then(function () {
|
||||||
delete req.session.user;
|
delete req.session.user;
|
||||||
|
res.set({
|
||||||
|
"X-Cache-Invalidate": "/*"
|
||||||
|
});
|
||||||
res.redirect('/ghost/signup/');
|
res.redirect('/ghost/signup/');
|
||||||
});
|
});
|
||||||
}, function resetFailure(error) {
|
}, function resetFailure(error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user