Fixed 404 handling for {admin url}/content/* routes

no issue

- added our theme error handling middleware to {admin}/content/ so that cache headers are properly set for 404s
- only registered {admin}/content when a separate admin url is configured so that we're not overriding {site}/content
This commit is contained in:
Kevin Ansfield 2019-09-11 14:18:31 +01:00
parent 6feeb67d84
commit 717567995b

View File

@ -44,20 +44,31 @@ module.exports = function setupParentApp(options = {}) {
// This sets global res.locals which are needed everywhere
parentApp.use(shared.middlewares.ghostLocals);
// Wrap the admin and API apps into a single express app for use with vhost
const adminApp = express();
adminApp.enable('trust proxy'); // required to respect x-forwarded-proto in admin requests
adminApp.use('/ghost/api', require('./api')());
adminApp.use('/ghost', require('./admin')());
// TODO: remove /content/* once we're sure the API is not returning relative asset URLs anywhere
adminApp.use(STATIC_IMAGE_URL_PREFIX, shared.middlewares.image.handleImageSizes, storage.getStorage().serve());
// Mount the apps on the parentApp
const adminHost = config.get('admin:url') ? (new URL(config.get('admin:url')).hostname) : '';
const frontendHost = new URL(config.get('url')).hostname;
const hasSeparateAdmin = adminHost && adminHost !== frontendHost;
// Wrap the admin and API apps into a single express app for use with vhost
const adminApp = express();
adminApp.enable('trust proxy'); // required to respect x-forwarded-proto in admin requests
adminApp.use('/ghost/api', require('./api')());
adminApp.use('/ghost', require('./admin')());
// TODO: remove {admin url}/content/* once we're sure the API is not returning relative asset URLs anywhere
// only register this route if the admin is separate so we're not overriding the {site}/content/* route
if (hasSeparateAdmin) {
adminApp.use(
STATIC_IMAGE_URL_PREFIX,
[
shared.middlewares.image.handleImageSizes,
storage.getStorage().serve(),
shared.middlewares.errorHandler.handleThemeResponse
]
);
}
// ADMIN + API
// with a separate admin url only serve on that host, otherwise serve on all hosts
const adminVhostArg = hasSeparateAdmin && adminHost ? adminHost : /.*/;