This commit is contained in:
Barış Soner Uşaklı
2021-06-18 12:36:04 -04:00
parent 9095057223
commit be01951c41

View File

@@ -17,10 +17,11 @@ module.exports = function (middleware) {
middleware.processRender = function processRender(req, res, next) { middleware.processRender = function processRender(req, res, next) {
// res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687 // res.render post-processing, modified from here: https://gist.github.com/mrlannigan/5051687
const { render } = res; const { render } = res;
async function renderMethod(template, options, fn) {
const self = this;
const req = this.req;
res.render = async function renderOverride(template, options, fn) {
const self = this;
const { req } = this;
async function renderMethod(template, options, fn) {
options = options || {}; options = options || {};
if (typeof options === 'function') { if (typeof options === 'function') {
fn = options; fn = options;
@@ -48,7 +49,7 @@ module.exports = function (middleware) {
tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags), tags: await meta.tags.parse(req, renderResult, res.locals.metaTags, res.locals.linkTags),
}; };
options.widgets = await widgets.render(req.uid, { options.widgets = await widgets.render(req.uid, {
template: template + '.tpl', template: `${template}.tpl`,
url: options.url, url: options.url,
templateData: options, templateData: options,
req: req, req: req,
@@ -71,14 +72,14 @@ module.exports = function (middleware) {
footer: renderHeaderFooter('renderFooter', req, res, options), footer: renderHeaderFooter('renderFooter', req, res, options),
}); });
const str = results.header + const str = `${results.header +
(res.locals.postHeader || '') + (res.locals.postHeader || '') +
results.content + results.content
'<script id="ajaxify-data" type="application/json">' + }<script id="ajaxify-data" type="application/json">${
JSON.stringify(options).replace(/<\//g, '<\\/') + JSON.stringify(options).replace(/<\//g, '<\\/')
'</script>' + }</script>${
(res.locals.preFooter || '') + res.locals.preFooter || ''
results.footer; }${results.footer}`;
if (typeof fn !== 'function') { if (typeof fn !== 'function') {
self.send(str); self.send(str);
@@ -87,7 +88,6 @@ module.exports = function (middleware) {
} }
} }
res.render = async function renderOverride(template, options, fn) {
try { try {
await renderMethod(template, options, fn); await renderMethod(template, options, fn);
} catch (err) { } catch (err) {