f4fb0fcbaa
no issue
- right now, we mount all API endpoints (v2, v3 and canary), alongside some
other routes, when Ghost is booting. This is wasteful because we don't
necessarily need any of the endpoints to get Ghost up and running
- even when Admin is used, it uses `canary` so `v2` and `v3` sit in memory
- the better approach here is to lazy load these endpoints, so they only
get mounted when needed
- this commit adds the `lazyUse` function into our Express lib,
which takes a mount path and a module function to execute down the
line. This gets passed to the wonderful `express-lazy-router` lib which
detects when we're calling an unmounted module and will mount it for
us
- from local testing, this speeds up boot time by about 18% and reduces
initial memory usage by about 6% 🚀
47 lines
1.4 KiB
JavaScript
47 lines
1.4 KiB
JavaScript
const debug = require('@tryghost/debug')('shared:express');
|
|
const express = require('express');
|
|
const {createLazyRouter} = require('express-lazy-router');
|
|
const sentry = require('./sentry');
|
|
|
|
const lazyLoad = createLazyRouter();
|
|
|
|
module.exports = (name) => {
|
|
debug('new app start', name);
|
|
const app = express();
|
|
app.set('name', name);
|
|
|
|
// Make sure 'req.secure' is valid for proxied requests
|
|
// (X-Forwarded-Proto header will be checked, if present)
|
|
app.enable('trust proxy');
|
|
|
|
// Sentry must be our first error handler. Mounting it here means all custom error handlers will come after
|
|
app.use(sentry.errorHandler);
|
|
|
|
app.lazyUse = function lazyUse(mountPath, requireFn) {
|
|
app.use(mountPath, lazyLoad(() => {
|
|
debug(`lazy-loading on ${mountPath}`);
|
|
return Promise.resolve(requireFn());
|
|
}));
|
|
};
|
|
|
|
debug('new app end', name);
|
|
return app;
|
|
};
|
|
|
|
// Wrap the main express router call
|
|
// This is mostly an experiement, and can likely be removed soon
|
|
module.exports.Router = (name, options) => {
|
|
debug('new Router start', name);
|
|
const router = express.Router(options);
|
|
|
|
router.use(sentry.errorHandler);
|
|
|
|
debug('new Router end', name);
|
|
return router;
|
|
};
|
|
|
|
module.exports.static = express.static;
|
|
|
|
// Export the OG module for testing based on the internals
|
|
module.exports._express = express;
|