Skip to content

Node.js based forum software built for the modern web

closed #1095 - js is now minified and saved to memory the first time it is requested (on prod mode), same with theme CSS

Julian Lam12e3e4539872
diff --git a/src/meta.js b/src/meta.js
index e494c4fccc..7a41b162ff 100644
--- a/src/meta.js
+++ b/src/meta.js
@@ -223,6 +223,7 @@ var fs = require('fs'),
};
Meta.js = {
+ cache: undefined,
scripts: [
'vendor/jquery/js/jquery.js',
'vendor/jquery/js/jquery-ui-1.10.4.custom.js',
@@ -240,7 +241,7 @@ var fs = require('fs'),
'src/translator.js',
'src/utils.js'
],
- minFile: path.join(__dirname, '..', 'public/src/nodebb.min.js'),
+ minFile: nconf.get('relative_path') + 'nodebb.min.js',
get: function (callback) {
plugins.fireHook('filter:scripts.get', this.scripts, function(err, scripts) {
var ctime,
@@ -265,46 +266,9 @@ var fs = require('fs'),
Meta.js.scripts = jsPaths.filter(function(path) { return path !== null });
- if (process.env.NODE_ENV !== 'development') {
- async.parallel({
- ctime: function (next) {
- async.map(jsPaths, fs.stat, function (err, stats) {
- async.reduce(stats, 0, function (memo, item, next) {
- if(item) {
- ctime = +new Date(item.ctime);
- next(null, ctime > memo ? ctime : memo);
- } else {
- next(null, memo);
- }
- }, next);
- });
- },
- minFile: function (next) {
- if (!fs.existsSync(Meta.js.minFile)) {
- winston.info('No minified client-side library found');
- return next(null, 0);
- }
-
- fs.stat(Meta.js.minFile, function (err, stat) {
- next(err, +new Date(stat.ctime));
- });
- }
- }, function (err, results) {
- if (results.minFile > results.ctime) {
- winston.info('No changes to client-side libraries -- skipping minification');
- callback(null, [path.relative(path.join(__dirname, '../public'), Meta.js.minFile)]);
- } else {
- winston.info('Minifying client-side libraries -- please wait');
- Meta.js.minify(function () {
- callback(null, [
- path.relative(path.join(__dirname, '../public'), Meta.js.minFile)
- ]);
- });
- }
- });
- } else {
- callback(null, scripts);
- }
+ callback(null, [
+ Meta.js.minFile
+ ]);
});
},
minify: function (callback) {
@@ -317,17 +281,8 @@ var fs = require('fs'),
}
minified = uglifyjs.minify(jsPaths);
- fs.writeFile(Meta.js.minFile, minified.code, function (err) {
- if (!err) {
- if (process.env.NODE_ENV === 'development') {
- winston.info('Minified client-side libraries');
- }
- callback();
- } else {
- winston.error('Problem minifying client-side libraries, exiting.');
- process.exit();
- }
- });
+ this.cache = minified.code;
+ callback();
}
};
diff --git a/src/routes/theme.js b/src/routes/meta.js
similarity index 85%
rename from src/routes/theme.js
rename to src/routes/meta.js
index e8a9052029..11a2d3d2fe 100644
--- a/src/routes/theme.js
+++ b/src/routes/meta.js
@@ -48,5 +48,15 @@ var path = require('path'),
}
});
});
+
+ app.get('/nodebb.min.js', function(req, res) {
+ if (meta.js.cache) {
+ res.type('text/javascript').send(meta.js.cache);
+ } else {
+ meta.js.minify(function() {
+ res.type('text/javascript').send(meta.js.cache);
+ });
+ }
+ });
};
})(exports);
\ No newline at end of file
diff --git a/src/webserver.js b/src/webserver.js
index 50fcae9035..9999c1a22e 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -33,7 +33,7 @@ var path = require('path'),
userRoute = require('./routes/user'),
apiRoute = require('./routes/api'),
feedsRoute = require('./routes/feeds'),
- themeRoute = require('./routes/theme');
+ metaRoute = require('./routes/meta');
if(nconf.get('ssl')) {
server = require('https').createServer({
@@ -483,7 +483,7 @@ process.on('uncaughtException', function(err) {
app.namespace(nconf.get('relative_path'), function () {
auth.registerApp(app);
- themeRoute.createRoutes(app);
+ metaRoute.createRoutes(app);
admin.createRoutes(app);
userRoute.createRoutes(app);
apiRoute.createRoutes(app);