caching compiled CSS locally, so repeated calls to the stylesheet serve from cache

This commit is contained in:
Julian Lam
2014-02-25 15:20:21 -05:00
parent 7f11d78b82
commit bc1c419722
2 changed files with 11 additions and 2 deletions

View File

@@ -337,6 +337,10 @@ var fs = require('fs'),
} }
}; };
Meta.css = {
cache: undefined
};
Meta.restart = function() { Meta.restart = function() {
if (process.send) { if (process.send) {
process.send('nodebb:restart'); process.send('nodebb:restart');

View File

@@ -9,6 +9,11 @@ var path = require('path'),
(function (Meta) { (function (Meta) {
Meta.createRoutes = function(app) { Meta.createRoutes = function(app) {
app.get('/stylesheet.css', function(req, res) { app.get('/stylesheet.css', function(req, res) {
if (meta.css.cache) {
res.type('text/css').send(200, meta.css.cache);
return;
}
db.getObjectFields('config', ['theme:type', 'theme:id'], function(err, themeData) { db.getObjectFields('config', ['theme:type', 'theme:id'], function(err, themeData) {
var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'), var themeId = (themeData['theme:id'] || 'nodebb-theme-vanilla'),
baseThemePath = path.join(nconf.get('themes_path'), themeId), baseThemePath = path.join(nconf.get('themes_path'), themeId),
@@ -31,11 +36,11 @@ var path = require('path'),
parser.parse(source, function(err, tree) { parser.parse(source, function(err, tree) {
if (err) { if (err) {
res.send(500, err.message); res.send(500, err.message);
console.log(err);
return; return;
} }
res.type('text/css').send(200, tree.toCSS()); meta.css.cache = tree.toCSS();
res.type('text/css').send(200, meta.css.cache);
}); });
} else { } else {
// Bootswatch theme not supported yet // Bootswatch theme not supported yet