diff --git a/core/boot.js b/core/boot.js index a490e62a7b..df99f63cc8 100644 --- a/core/boot.js +++ b/core/boot.js @@ -145,11 +145,10 @@ async function initDynamicRouting() { debug('Begin: Dynamic Routing'); const routing = require('./frontend/services/routing'); const bridge = require('./bridge'); - // We pass the frontend API version + the dynamic routes here, so that the frontend services are slightly less tightly-coupled + // We pass the frontend API version here, so that the frontend services are slightly less tightly-coupled + routing.bootstrap.start(bridge.getFrontendApiVersion()); const settings = require('./server/services/settings'); const frontendSettings = require('./frontend/services/settings'); - const dynamicRoutes = frontendSettings.get('routes'); - routing.bootstrap.start(bridge.getFrontendApiVersion(), dynamicRoutes); const getRoutesHash = () => frontendSettings.getCurrentHash('routes'); await settings.syncRoutesHash(getRoutesHash); debug('End: Dynamic Routing'); diff --git a/core/frontend/services/routing/bootstrap.js b/core/frontend/services/routing/bootstrap.js index e983873857..8a0ab3279c 100644 --- a/core/frontend/services/routing/bootstrap.js +++ b/core/frontend/services/routing/bootstrap.js @@ -1,7 +1,7 @@ const debug = require('@tryghost/debug')('services:routing:bootstrap'); const _ = require('lodash'); const events = require('../../../server/lib/common/events'); -const frontendSettings = require('../settings'); +const settingsService = require('../settings'); const StaticRoutesRouter = require('./StaticRoutesRouter'); const StaticPagesRouter = require('./StaticPagesRouter'); const CollectionRouter = require('./CollectionRouter'); @@ -22,7 +22,8 @@ let siteRouter; * CASES: * - if Ghost starts, it will first init the site app with the wrapper router and then call `start` * separately, because it could be that your blog goes into maintenance mode - * - if you change your route settings, we will re-initialise routing + * - if you upload your routes.yaml in the admin client, we will re-initialise routing + * - * * @param {Object} options * @returns {ExpressRouter} @@ -40,16 +41,14 @@ module.exports.init = (options = {start: false}) => { if (options.start) { let apiVersion = _.isBoolean(options.start) ? defaultApiVersion : options.start; - // NOTE: Get the routes.yaml config - const dynamicRoutes = frontendSettings.get('routes'); - this.start(apiVersion, dynamicRoutes); + this.start(apiVersion); } return siteRouter.router(); }; /** - * @description This function will create the routers based on the route settings + * @description This function will create the routers based on the routes.yaml config. * * The routers are created in a specific order. This order defines who can get a resource first or * who can dominant other routers. @@ -60,11 +59,8 @@ module.exports.init = (options = {start: false}) => { * 4. Collections * 5. Static Pages: Weaker than collections, because we first try to find a post slug and fallback to lookup a static page. * 6. Internal Apps: Weakest - * - * @param {string} apiVersion - * @param {object} dynamicRoutes */ -module.exports.start = (apiVersion, dynamicRoutes) => { +module.exports.start = (apiVersion) => { const RESOURCE_CONFIG = require(`./config/${apiVersion}`); const unsubscribeRouter = new UnsubscribeRouter(); @@ -75,8 +71,11 @@ module.exports.start = (apiVersion, dynamicRoutes) => { siteRouter.mountRouter(previewRouter.router()); registry.setRouter('previewRouter', previewRouter); + // NOTE: Get the routes.yaml config + const dynamicRoutes = settingsService.get('routes'); + _.each(dynamicRoutes.routes, (value, key) => { - const staticRoutesRouter = new StaticRoutesRouter(key, value); + const staticRoutesRouter = new StaticRoutesRouter(key, value, RESOURCE_CONFIG); siteRouter.mountRouter(staticRoutesRouter.router()); registry.setRouter(staticRoutesRouter.identifier, staticRoutesRouter); diff --git a/core/server/api/v3/settings.js b/core/server/api/v3/settings.js index b1138d19e9..7be99a2839 100644 --- a/core/server/api/v3/settings.js +++ b/core/server/api/v3/settings.js @@ -313,7 +313,7 @@ module.exports = { method: 'edit' }, async query(frame) { - await routeSettings.setFromFilePath(frame.file.path); + await routeSettings.settings.setFromFilePath(frame.file.path); const getRoutesHash = () => frontendSettings.getCurrentHash('routes'); await settingsService.syncRoutesHash(getRoutesHash); } @@ -333,7 +333,7 @@ module.exports = { method: 'browse' }, query() { - return routeSettings.get(); + return routeSettings.settings.get(); } } };