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
This commit is contained in:
Hannah Wolfe 2021-06-27 13:38:48 +01:00
parent 81cde2bbf9
commit 2c729e99f9
No known key found for this signature in database
GPG Key ID: 9F8C7532D0A6BA55
3 changed files with 13 additions and 9 deletions

View File

@ -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();

View File

@ -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(() => {

View File

@ -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)