From dbbbe218831f85073e4dfb5d89109fa198c6698e Mon Sep 17 00:00:00 2001 From: psychobunny Date: Sun, 2 Mar 2014 15:14:38 -0500 Subject: [PATCH] re-implementing meta and link tags part 1 --- src/controllers/categories.js | 8 +- src/controllers/topics.js | 138 +++++++++++++++++----------------- src/middleware/index.js | 13 ---- src/middleware/middleware.js | 40 +++++----- 4 files changed, 92 insertions(+), 107 deletions(-) diff --git a/src/controllers/categories.js b/src/controllers/categories.js index 90666dc744..dc51672662 100644 --- a/src/controllers/categories.js +++ b/src/controllers/categories.js @@ -3,10 +3,10 @@ var categoriesController = {}, async = require('async'), qs = require('querystring'), - categoryTools = require('../categoryTools'), - user = require('../user'), - categories = require('../categories'), - topics = require('../topics'); + categoryTools = require('./../categoryTools'), + user = require('./../user'), + categories = require('./../categories'), + topics = require('./../topics'); categoriesController.recent = function(req, res, next) { var uid = (req.user) ? req.user.uid : 0; diff --git a/src/controllers/topics.js b/src/controllers/topics.js index 34f3022eb7..3b975b673f 100644 --- a/src/controllers/topics.js +++ b/src/controllers/topics.js @@ -1,11 +1,16 @@ +"use strict"; + var topicsController = {}, async = require('async'), S = require('string'), validator = require('validator'), + nconf = require('nconf'), qs = require('querystring'), - user = require('../user'), - topics = require('../topics'), - threadTools = require('../threadTools'); + user = require('./../user'), + meta = require('./../meta'), + topics = require('./../topics'), + threadTools = require('./../threadTools'), + utils = require('./../../public/src/utils'); topicsController.get = function(req, res, next) { var tid = req.params.topic_id, @@ -72,77 +77,70 @@ topicsController.get = function(req, res, next) { next(null, topicData); - /*var ogImageUrl = meta.config['brand:logo']; + var ogImageUrl = meta.config['brand:logo']; if(ogImageUrl && ogImageUrl.indexOf('http') === -1) { ogImageUrl = nconf.get('url') + ogImageUrl; } - app.build_header({ - req: req, - res: res, - metaTags: [ - { - name: "title", - content: topicData.title - }, - { - name: "description", - content: description - }, - { - property: 'og:title', - content: topicData.title - }, - { - property: 'og:description', - content: description - }, - { - property: "og:type", - content: 'article' - }, - { - property: "og:url", - content: nconf.get('url') + '/topic/' + topicData.slug - }, - { - property: "og:image:url", - content: ogImageUrl - }, - { - property: 'og:image', - content: topicData.posts.length?topicData.posts[0].picture:'' - }, - { - property: "article:published_time", - content: utils.toISOString(topicData.timestamp) - }, - { - property: 'article:modified_time', - content: utils.toISOString(lastMod) - }, - { - property: 'article:section', - content: topicData.category.name - } - ], - linkTags: [ - { - rel: 'alternate', - type: 'application/rss+xml', - href: nconf.get('url') + '/topic/' + tid + '.rss' - }, - { - rel: 'up', - href: nconf.get('url') + '/category/' + topicData.category.slug - } - ] - }, function (err, header) { - next(err, { - header: header, - posts: topicData - }); - });*/ + res.locals.metaTags = [ + { + name: "title", + content: topicData.title + }, + { + name: "description", + content: description + }, + { + property: 'og:title', + content: topicData.title + }, + { + property: 'og:description', + content: description + }, + { + property: "og:type", + content: 'article' + }, + { + property: "og:url", + content: nconf.get('url') + '/topic/' + topicData.slug + }, + { + property: "og:image:url", + content: ogImageUrl + }, + { + property: 'og:image', + content: topicData.posts.length?topicData.posts[0].picture:'' + }, + { + property: "article:published_time", + content: utils.toISOString(topicData.timestamp) + }, + { + property: 'article:modified_time', + content: utils.toISOString(lastMod) + }, + { + property: 'article:section', + content: topicData.category.name + } + ]; + + res.locals.linkTags = [ + { + rel: 'alternate', + type: 'application/rss+xml', + href: nconf.get('url') + '/topic/' + tid + '.rss' + }, + { + rel: 'up', + href: nconf.get('url') + '/category/' + topicData.category.slug + } + ]; + } ], function (err, posts) { if (err) { diff --git a/src/middleware/index.js b/src/middleware/index.js index 371e04ae1b..a27a632517 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -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 })); diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 6863310746..b3cc261f84 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -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);