From 539693a4a48236864a8adbe35042e69af0a871fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 15 Jan 2025 10:15:02 -0500 Subject: [PATCH] perf: closes #13052, only call loadConfig once per page when rendering widgets --- src/widgets/index.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/widgets/index.js b/src/widgets/index.js index 67ab20607b..2750d25232 100644 --- a/src/widgets/index.js +++ b/src/widgets/index.js @@ -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) {