Ghost/core/server/services/routing/controllers/entry.js
Katharina Irrgang 7b0d5d465b 🐛 Fixed preview url and Zapier on subdirectory (#9683)
closes #9675

- with dynamic routing we have introduced a breaking change, which we have overseen
- Ghost does not return absolute urls, that's why the clients need to concat the blog url and the resource url
- with 1.24.0 Ghost returned resource urls including the subdirectory
- this caused trouble for e.g. zapier or the preview feature in the admin client
- revert breaking change and ensure we only expose resource urls without subdirectory
2018-06-12 16:36:58 +01:00

83 lines
3.0 KiB
JavaScript

const debug = require('ghost-ignition').debug('services:routing:controllers:entry'),
url = require('url'),
urlService = require('../../url'),
filters = require('../../../filters'),
helpers = require('../helpers');
/**
* @TODO:
* - use `filter` for `findOne`?
* - always execute `next` until no router want's to serve and 404's
*/
module.exports = function entryController(req, res, next) {
debug('entryController', res.locals.routerOptions);
return helpers.postLookup(req.path, res.locals.routerOptions)
.then(function then(lookup) {
// Format data 1
const post = lookup ? lookup.post : false;
if (!post) {
debug('no post');
return next();
}
// CASE: postlookup can detect options for example /edit, unknown options get ignored and end in 404
if (lookup.isUnknownOption) {
debug('isUnknownOption');
return next();
}
// CASE: last param is of url is /edit, redirect to admin
if (lookup.isEditURL) {
debug('redirect. is edit url');
return urlService.utils.redirectToAdmin(302, res, '/editor/' + post.id);
}
/**
* CASE: check if type of router owns this resource
*
* Static pages have a hardcoded permalink, which is `/:slug/`.
* Imagine you define a collection under `/` with the permalink `/:slug/`.
*
* The router hierarchy is:
*
* 1. collections
* 2. static pages
*
* Both permalinks are registered in express. If you serve a static page, the
* collection router will try to serve this as a post resource.
*
* That's why we have to check against the router type.
*/
if (urlService.getResourceById(post.id).config.type !== res.locals.routerOptions.type) {
debug('not my resource type');
return next();
}
/**
* CASE: Permalink is not valid anymore, we redirect him permanently to the correct one
* This should only happen if you have date permalinks enabled and you change
* your publish date.
*
* @NOTE:
*
* Ensure we redirect to the correct post url including subdirectory.
*/
if (post.url !== req.path) {
debug('redirect');
return urlService.utils.redirect301(res, url.format({
pathname: urlService.utils.createUrl(post.url, false, false, true),
search: url.parse(req.originalUrl).search
}));
}
helpers.secure(req, post);
filters.doFilter('prePostsRender', post, res.locals)
.then(helpers.renderEntry(req, res));
})
.catch(helpers.handleError(next));
};