Ghost/core/server/controllers/single.js
Hannah Wolfe 9f9d8b2fec Always use our redirects utility (#9132)
no issue

- This started as an attempt to simplify the admin redirect code
- I realised we were sometimes using utils.redirect301 and sometimes not
- Decided to move this into utils.url as it's more relevant to URL generation
- Unified usage of redirects in the codebase
- Updated tests & ensured we have basic coverage
- rename adminRedirect -> redirectToAdmin
- Tweak method signature, fix channel edit redirects
- Tests: Optimised test descriptions for url-redirects_spec.js
- ensure caching works as expected
2017-10-12 14:36:50 +02:00

38 lines
1.3 KiB
JavaScript

var utils = require('../utils'),
filters = require('../filters'),
handleError = require('./frontend/error'),
postLookup = require('./frontend/post-lookup'),
renderPost = require('./frontend/render-post'),
setRequestIsSecure = require('./frontend/secure');
module.exports = function single(req, res, next) {
// Query database to find post
return postLookup(req.path).then(function then(lookup) {
var post = lookup ? lookup.post : false;
if (!post) {
return next();
}
// CASE: postlookup can detect options for example /edit, unknown options get ignored and end in 404
if (lookup.isUnknownOption) {
return next();
}
// CASE: last param is of url is /edit, redirect to admin
if (lookup.isEditURL) {
return utils.url.redirectToAdmin(302, res, '#/editor/' + post.id);
}
// CASE: permalink is not valid anymore, we redirect him permanently to the correct one
if (post.url !== req.path) {
return utils.url.redirect301(res, post.url);
}
setRequestIsSecure(req, post);
filters.doFilter('prePostsRender', post, res.locals)
.then(renderPost(req, res));
}).catch(handleError(next));
};