mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-07-06 10:09:38 +02:00
Use Benchpress (#5901)
* Use Benchpress * Use Benchpress.compileParse * Error for template load failure * Use benchpressjs package * Compile templates on demand * Fix user settings page * Fix admin search to exclude `.jst` files * Fix 500-embed So ajaxify can still show an error if the server goes down
This commit is contained in:
committed by
Barış Soner Uşaklı
parent
864321f727
commit
abffc29128
@@ -2,11 +2,13 @@
|
||||
|
||||
var async = require('async');
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
var csrf = require('csurf');
|
||||
var validator = require('validator');
|
||||
var nconf = require('nconf');
|
||||
var ensureLoggedIn = require('connect-ensure-login');
|
||||
var toobusy = require('toobusy-js');
|
||||
var Benchpress = require('benchpressjs');
|
||||
|
||||
var plugins = require('../plugins');
|
||||
var meta = require('../meta');
|
||||
@@ -186,3 +188,33 @@ middleware.delayLoading = function (req, res, next) {
|
||||
// Introduces an artificial delay during load so that brute force attacks are effectively mitigated
|
||||
setTimeout(next, 1000);
|
||||
};
|
||||
|
||||
var viewsDir = nconf.get('views_dir');
|
||||
middleware.templatesOnDemand = function (req, res, next) {
|
||||
var filePath = req.filePath || path.join(viewsDir, req.path);
|
||||
if (!filePath.endsWith('.jst')) {
|
||||
return next();
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (cb) {
|
||||
file.exists(filePath, cb);
|
||||
},
|
||||
function (exists, cb) {
|
||||
if (exists) {
|
||||
return next();
|
||||
}
|
||||
|
||||
fs.readFile(filePath.replace(/\.jst$/, '.tpl'), cb);
|
||||
},
|
||||
function (source, cb) {
|
||||
Benchpress.precompile({
|
||||
source: source.toString(),
|
||||
minify: global.env !== 'development',
|
||||
}, cb);
|
||||
},
|
||||
function (compiled, cb) {
|
||||
fs.writeFile(filePath, compiled, cb);
|
||||
},
|
||||
], next);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user