mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-28 01:21:13 +01:00
perf: closes #13052, only call loadConfig once per page
when rendering widgets
This commit is contained in:
@@ -22,7 +22,12 @@ widgets.render = async function (uid, options) {
|
|||||||
|
|
||||||
const locations = _.uniq(Object.keys(data.global).concat(Object.keys(data[options.template])));
|
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 = {};
|
const returnData = {};
|
||||||
locations.forEach((location, i) => {
|
locations.forEach((location, i) => {
|
||||||
@@ -34,7 +39,7 @@ widgets.render = async function (uid, options) {
|
|||||||
return returnData;
|
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] || []);
|
const widgetsAtLocation = (data[options.template][location] || []).concat(data.global[location] || []);
|
||||||
|
|
||||||
if (!widgetsAtLocation.length) {
|
if (!widgetsAtLocation.length) {
|
||||||
@@ -42,12 +47,12 @@ async function renderLocation(location, data, uid, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const renderedWidgets = await Promise.all(
|
const renderedWidgets = await Promise.all(
|
||||||
widgetsAtLocation.map(widget => renderWidget(widget, uid, options, location))
|
widgetsAtLocation.map(widget => renderWidget(widget, uid, options, config, location))
|
||||||
);
|
);
|
||||||
return renderedWidgets;
|
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)) {
|
if (!widget || !widget.data || (!!widget.data['hide-mobile'] && options.req.useragent.isMobile)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -57,12 +62,6 @@ async function renderWidget(widget, uid, options, location) {
|
|||||||
return;
|
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 });
|
const templateData = _.assign({ }, options.templateData, { config: config });
|
||||||
try {
|
try {
|
||||||
const data = await plugins.hooks.fire(`filter:widget.render:${widget.widget}`, {
|
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,
|
res: options.res,
|
||||||
location,
|
location,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { html } = data;
|
let { html } = data;
|
||||||
|
|
||||||
if (widget.data.container && widget.data.container.match('{body}')) {
|
if (widget.data.container && widget.data.container.match('{body}')) {
|
||||||
html = await Benchpress.compileRender(widget.data.container, {
|
html = await Benchpress.compileRender(widget.data.container, {
|
||||||
title: widget.data.title,
|
title: widget.data.title,
|
||||||
@@ -88,16 +87,16 @@ async function renderWidget(widget, uid, options, location) {
|
|||||||
template: data.templateData && data.templateData.template,
|
template: data.templateData && data.templateData.template,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (html) {
|
if (html) {
|
||||||
html = await translator.translate(html, userLang);
|
html = await translator.translate(html, config.userLang || meta.config.defaultLang || 'en-GB');
|
||||||
}
|
}
|
||||||
|
|
||||||
return { html };
|
return { html };
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
winston.error(err.stack);
|
winston.error(err.stack);
|
||||||
return { html: '' }
|
return { html: '' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
widgets.checkVisibility = async function (data, uid) {
|
widgets.checkVisibility = async function (data, uid) {
|
||||||
|
|||||||
Reference in New Issue
Block a user