Defer pulibc and uplaod middlewares

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-03-30 16:05:29 +02:00
parent 5a259ddf41
commit b3a6114328
2 changed files with 15 additions and 13 deletions

View File

@ -22,6 +22,6 @@ module.exports = strapi => ({
strapi.app.onerror(err);
});
strapi.router.get('/uploads/(.*)', range, koaStatic(staticDir));
strapi.router.get('/uploads/(.*)', range, koaStatic(staticDir, { defer: true }));
},
});

View File

@ -27,21 +27,14 @@ module.exports = strapi => {
const { defaultIndex, maxAge, path: publicPath } = strapi.config.middleware.settings.public;
const staticDir = path.resolve(strapi.dir, publicPath || strapi.config.paths.static);
// Match every route with an extension.
// The file without extension will not be served.
// Note: This route can be overriden by the user.
strapi.router.get(
'/(.*)',
koaStatic(staticDir, {
maxage: maxAge,
defer: false,
})
);
if (defaultIndex === true) {
const index = fs.readFileSync(path.join(__dirname, 'index.html'), 'utf8');
const serveIndexPage = async ctx => {
const serveIndexPage = async (ctx, next) => {
// defer rendering of strapi index page
await next();
if (ctx.body != null || ctx.status !== 404) return;
ctx.url = 'index.html';
const isInitialised = await utils.isInitialised(strapi);
const data = {
@ -70,6 +63,15 @@ module.exports = strapi => {
strapi.router.get('/index.html', serveIndexPage);
}
// serve files in public folder unless a sub router renders something else
strapi.router.get(
'/(.*)',
koaStatic(staticDir, {
maxage: maxAge,
defer: true,
})
);
if (!strapi.config.serveAdminPanel) return;
const basename = _.get(strapi.config.currentEnvironment.server, 'admin.path')