mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
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:
parent
81cde2bbf9
commit
2c729e99f9
3 changed files with 13 additions and 9 deletions
|
@ -53,8 +53,7 @@ class Bridge {
|
||||||
|
|
||||||
if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) {
|
if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) {
|
||||||
events.emit('services.themes.api.changed');
|
events.emit('services.themes.api.changed');
|
||||||
const siteApp = require('./server/web/site/app');
|
this.reloadFrontend();
|
||||||
siteApp.reload();
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logging.error(new errors.InternalServerError({
|
logging.error(new errors.InternalServerError({
|
||||||
|
@ -71,6 +70,12 @@ class Bridge {
|
||||||
return config.get('api:versions:default');
|
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();
|
const bridge = new Bridge();
|
||||||
|
|
|
@ -6,6 +6,7 @@ const urlService = require('../url');
|
||||||
|
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
const config = require('../../../shared/config');
|
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
|
* 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)
|
* - 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
|
* - then we reload the whole site app, which will reset all routers and re-create the url generators
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const setFromFilePath = (filePath) => {
|
const setFromFilePath = (filePath) => {
|
||||||
const settingsPath = config.getContentPath('settings');
|
const settingsPath = config.getContentPath('settings');
|
||||||
const backupRoutesPath = path.join(settingsPath, `routes-${moment().format('YYYY-MM-DD-HH-mm-ss')}.yaml`);
|
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});
|
urlService.resetGenerators({releaseResourcesOnly: true});
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const siteApp = require('../../../server/web/site/app');
|
|
||||||
|
|
||||||
const bringBackValidRoutes = () => {
|
const bringBackValidRoutes = () => {
|
||||||
urlService.resetGenerators({releaseResourcesOnly: true});
|
urlService.resetGenerators({releaseResourcesOnly: true});
|
||||||
|
|
||||||
return fs.copy(backupRoutesPath, `${settingsPath}/routes.yaml`)
|
return fs.copy(backupRoutesPath, `${settingsPath}/routes.yaml`)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return siteApp.reload();
|
return bridge.reloadFrontend();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
siteApp.reload();
|
bridge.reloadFrontend();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return bringBackValidRoutes()
|
return bringBackValidRoutes()
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
|
|
|
@ -6,7 +6,6 @@ const {URL} = require('url');
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
|
|
||||||
// App requires
|
// App requires
|
||||||
const bridge = require('../../../bridge');
|
|
||||||
const config = require('../../../shared/config');
|
const config = require('../../../shared/config');
|
||||||
const constants = require('@tryghost/constants');
|
const constants = require('@tryghost/constants');
|
||||||
const storage = require('../../adapters/storage');
|
const storage = require('../../adapters/storage');
|
||||||
|
@ -190,9 +189,9 @@ module.exports = function setupSiteApp(options = {}) {
|
||||||
return siteApp;
|
return siteApp;
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports.reload = () => {
|
module.exports.reload = ({apiVersion}) => {
|
||||||
// https://github.com/expressjs/express/issues/2596
|
// https://github.com/expressjs/express/issues/2596
|
||||||
router = siteRoutes({start: bridge.getFrontendApiVersion()});
|
router = siteRoutes({start: apiVersion});
|
||||||
Object.setPrototypeOf(SiteRouter, router);
|
Object.setPrototypeOf(SiteRouter, router);
|
||||||
|
|
||||||
// re-initialse apps (register app routers, because we have re-initialised the site routers)
|
// re-initialse apps (register app routers, because we have re-initialised the site routers)
|
||||||
|
|
Loading…
Add table
Reference in a new issue