re-implementing meta and link tags part 1

This commit is contained in:
psychobunny
2014-03-02 15:14:38 -05:00
parent 6820e9e662
commit dbbbe21883
4 changed files with 92 additions and 107 deletions

View File

@@ -102,20 +102,16 @@ function catch404(req, res, next) {
res.status(404);
if (isClientScript.test(req.url)) {
// Handle missing client-side scripts
res.type('text/javascript').send(200, '');
} else if (isLanguage.test(req.url)) {
// Handle languages by sending an empty object
res.json(200, {});
} else if (req.accepts('html')) {
// respond with html page
if (process.env.NODE_ENV === 'development') {
winston.warn('Route requested but not found: ' + req.url);
}
res.redirect(nconf.get('relative_path') + '/404');
} else if (req.accepts('json')) {
// respond with json
if (process.env.NODE_ENV === 'development') {
winston.warn('Route requested but not found: ' + req.url);
}
@@ -124,7 +120,6 @@ function catch404(req, res, next) {
error: 'Not found'
});
} else {
// default to plain-text. send()
res.type('txt').send('Not found');
}
}
@@ -137,7 +132,6 @@ module.exports = function(app, data) {
app.set('view engine', 'tpl');
app.set('views', path.join(__dirname, '../../public/templates'));
// Pre-router middlewares
app.use(express.compress());
app.use(express.favicon(path.join(__dirname, '../../', 'public', meta.config['brand:favicon'] ? meta.config['brand:favicon'] : 'favicon.ico')));
@@ -163,28 +157,21 @@ module.exports = function(app, data) {
app.use(express.csrf()); // todo, make this a conditional middleware
// Local vars, other assorted setup
app.use(function (req, res, next) {
res.locals.csrf_token = req.session._csrf;
// Disable framing
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
next();
});
app.use(middleware.processRender);
// Authentication Routes
auth.initialize(app);
routeCurrentTheme(app, data.currentThemeData);
// Route paths to screenshots for installed themes
routeThemeScreenshots(app, data.themesData);
app.use(app.router);
// Static directory /public
app.use(nconf.get('relative_path'), express.static(path.join(__dirname, '../../', 'public'), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));

View File

@@ -6,6 +6,7 @@ var app,
middleware = {},
async = require('async'),
path = require('path'),
winston = require('winston'),
validator = require('validator'),
fs = require('fs'),
nconf = require('nconf'),
@@ -89,13 +90,8 @@ middleware.buildHeader = function(req, res, next) {
async.parallel([
function(next) {
// temp, don't forget to set metaTags and linkTags to res.locals.header
middleware.build_header({
req: req,
res: res
}, function(err, template) {
res.locals.header = template;
next(err);
});
res.locals.header = true;
next();
},
function(next) {
// this is slower than the original implementation because the rendered template is not cached
@@ -115,10 +111,11 @@ middleware.buildHeader = function(req, res, next) {
};
/**
* TODO: switch signature to req, res, callback
* `options` object requires: req, res
* accepts: metaTags, linkTags
*/
middleware.build_header = function (options, callback) {
middleware.renderHeader = function (options, callback) {
var custom_header = {
'navigation': []
};
@@ -168,9 +165,9 @@ middleware.build_header = function (options, callback) {
};
var uid = '0';
console.log(options.res.locals.metaTags);
// Meta Tags
/*templateValues.metaTags = defaultMetaTags.concat(options.metaTags || []).map(function(tag) {
templateValues.metaTags = defaultMetaTags.concat(options.res.locals.metaTags || []).map(function(tag) {
if(!tag || typeof tag.content !== 'string') {
winston.warn('Invalid meta tag. ', tag);
return tag;
@@ -180,7 +177,7 @@ middleware.build_header = function (options, callback) {
return escapeList[tag] || tag;
});
return tag;
});*/
});
// Link Tags
/*templateValues.linkTags = defaultLinkTags.concat(options.linkTags || []);
@@ -204,7 +201,7 @@ middleware.build_header = function (options, callback) {
async.parallel([
function(next) {
translator.get('pages:' + path.basename(options.req.url), function(translated) {
/*var metaTitle = templateValues.metaTags.filter(function(tag) {
var metaTitle = templateValues.metaTags.filter(function(tag) {
return tag.name === 'title';
});
if (translated) {
@@ -213,7 +210,7 @@ middleware.build_header = function (options, callback) {
templateValues.browserTitle = metaTitle[0].content;
} else {
templateValues.browserTitle = meta.config.browserTitle || 'NodeBB';
}*/
}
next();
});
@@ -260,17 +257,20 @@ middleware.processRender = function(req, res, next) {
}
render.call(self, template, options, function(err, str) {
if (res.locals.header) {
str = res.locals.header + str;
}
if (res.locals.footer) {
str = str + res.locals.footer;
}
if (str) {
translator.translate(str, function(translated) {
fn(err, translated);
if (res.locals.header) {
middleware.renderHeader({
req: req,
res: res
}, function(err, template) {
str = template + str;
translator.translate(str, function(translated) {
fn(err, translated);
});
});
} else {
fn(err, str);