perf: closes #13052, only call loadConfig once per page

when rendering widgets
This commit is contained in:
Barış Soner Uşaklı
2025-01-15 10:15:02 -05:00
parent 168e6b2c92
commit 539693a4a4

View File

@@ -22,7 +22,12 @@ widgets.render = async function (uid, options) {
const locations = _.uniq(Object.keys(data.global).concat(Object.keys(data[options.template])));
const widgetData = await Promise.all(locations.map(location => renderLocation(location, data, uid, options)));
let config = options.res.locals.config || {};
if (options.res.locals.isAPI) {
config = await apiController.loadConfig(options.req);
}
const widgetData = await Promise.all(locations.map(location => renderLocation(location, data, uid, options, config)));
const returnData = {};
locations.forEach((location, i) => {
@@ -34,7 +39,7 @@ widgets.render = async function (uid, options) {
return returnData;
};
async function renderLocation(location, data, uid, options) {
async function renderLocation(location, data, uid, options, config) {
const widgetsAtLocation = (data[options.template][location] || []).concat(data.global[location] || []);
if (!widgetsAtLocation.length) {
@@ -42,12 +47,12 @@ async function renderLocation(location, data, uid, options) {
}
const renderedWidgets = await Promise.all(
widgetsAtLocation.map(widget => renderWidget(widget, uid, options, location))
widgetsAtLocation.map(widget => renderWidget(widget, uid, options, config, location))
);
return renderedWidgets;
}
async function renderWidget(widget, uid, options, location) {
async function renderWidget(widget, uid, options, config, location) {
if (!widget || !widget.data || (!!widget.data['hide-mobile'] && options.req.useragent.isMobile)) {
return;
}
@@ -57,12 +62,6 @@ async function renderWidget(widget, uid, options, location) {
return;
}
let config = options.res.locals.config || {};
if (options.res.locals.isAPI) {
config = await apiController.loadConfig(options.req);
}
const userLang = config.userLang || meta.config.defaultLang || 'en-GB';
const templateData = _.assign({ }, options.templateData, { config: config });
try {
const data = await plugins.hooks.fire(`filter:widget.render:${widget.widget}`, {
@@ -74,13 +73,13 @@ async function renderWidget(widget, uid, options, location) {
res: options.res,
location,
});
if (!data) {
return;
}
let { html } = data;
if (widget.data.container && widget.data.container.match('{body}')) {
html = await Benchpress.compileRender(widget.data.container, {
title: widget.data.title,
@@ -88,16 +87,16 @@ async function renderWidget(widget, uid, options, location) {
template: data.templateData && data.templateData.template,
});
}
if (html) {
html = await translator.translate(html, userLang);
html = await translator.translate(html, config.userLang || meta.config.defaultLang || 'en-GB');
}
return { html };
} catch (err) {
winston.error(err.stack);
return { html: '' }
}
return { html: '' };
}
}
widgets.checkVisibility = async function (data, uid) {