From 2c729e99f9730b8542e9c33a75d27ea1fb46bf26 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Sun, 27 Jun 2021 13:38:48 +0100 Subject: [PATCH] Added reload frontend wrapper to bridge - Allows for slight decoupling of API and frontend with route settings being updated - Activate theme now calls the same codepath to reload the frontend - Yet another step on the path to make it possible to init/reload/run the frontend independently from the server --- core/bridge.js | 9 +++++++-- core/frontend/services/routing/settings.js | 8 ++++---- core/server/web/site/app.js | 5 ++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/core/bridge.js b/core/bridge.js index 6a8ede8608..0eb4956283 100644 --- a/core/bridge.js +++ b/core/bridge.js @@ -53,8 +53,7 @@ class Bridge { if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) { events.emit('services.themes.api.changed'); - const siteApp = require('./server/web/site/app'); - siteApp.reload(); + this.reloadFrontend(); } } catch (err) { logging.error(new errors.InternalServerError({ @@ -71,6 +70,12 @@ class Bridge { return config.get('api:versions:default'); } } + + reloadFrontend() { + const apiVersion = this.getFrontendApiVersion(); + const siteApp = require('./server/web/site/app'); + siteApp.reload({apiVersion}); + } } const bridge = new Bridge(); diff --git a/core/frontend/services/routing/settings.js b/core/frontend/services/routing/settings.js index 0aa135072d..d2ae407ba4 100644 --- a/core/frontend/services/routing/settings.js +++ b/core/frontend/services/routing/settings.js @@ -6,6 +6,7 @@ const urlService = require('../url'); const errors = require('@tryghost/errors'); const config = require('../../../shared/config'); +const bridge = require('../../../bridge'); /** * The `routes.yaml` file offers a way to configure your Ghost blog. It's currently a setting feature @@ -18,6 +19,7 @@ const config = require('../../../shared/config'); * - we don't destroy the resources, we only release them (this avoids reloading all resources from the db again) * - then we reload the whole site app, which will reset all routers and re-create the url generators */ + const setFromFilePath = (filePath) => { const settingsPath = config.getContentPath('settings'); const backupRoutesPath = path.join(settingsPath, `routes-${moment().format('YYYY-MM-DD-HH-mm-ss')}.yaml`); @@ -30,19 +32,17 @@ const setFromFilePath = (filePath) => { urlService.resetGenerators({releaseResourcesOnly: true}); }) .then(() => { - const siteApp = require('../../../server/web/site/app'); - const bringBackValidRoutes = () => { urlService.resetGenerators({releaseResourcesOnly: true}); return fs.copy(backupRoutesPath, `${settingsPath}/routes.yaml`) .then(() => { - return siteApp.reload(); + return bridge.reloadFrontend(); }); }; try { - siteApp.reload(); + bridge.reloadFrontend(); } catch (err) { return bringBackValidRoutes() .finally(() => { diff --git a/core/server/web/site/app.js b/core/server/web/site/app.js index 814c831dbe..6cd0897d92 100644 --- a/core/server/web/site/app.js +++ b/core/server/web/site/app.js @@ -6,7 +6,6 @@ const {URL} = require('url'); const errors = require('@tryghost/errors'); // App requires -const bridge = require('../../../bridge'); const config = require('../../../shared/config'); const constants = require('@tryghost/constants'); const storage = require('../../adapters/storage'); @@ -190,9 +189,9 @@ module.exports = function setupSiteApp(options = {}) { return siteApp; }; -module.exports.reload = () => { +module.exports.reload = ({apiVersion}) => { // https://github.com/expressjs/express/issues/2596 - router = siteRoutes({start: bridge.getFrontendApiVersion()}); + router = siteRoutes({start: apiVersion}); Object.setPrototypeOf(SiteRouter, router); // re-initialse apps (register app routers, because we have re-initialised the site routers)