Created
October 31, 2014 17:51
-
-
Save paton/1a573fd7c178bfb9156f to your computer and use it in GitHub Desktop.
Zombie / SEO middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = requireBase('libs/app'), | |
Zombie = require('zombie'); | |
var botRegex = /bot|crawler|baiduspider|80legs|mediapartners-google|adsbot-google/i; | |
module.exports = function(req, res, next) { | |
// Zombie not necessary for homepage since it's boostrapped | |
// Exclude non-get calls and API calls | |
if (req.url === '/' || req.method !== 'GET' || /^\/api\//.test(req.url)) return next(); | |
// Check if the requester is a bot | |
if (!botRegex.test(req.headers['user-agent'] || '')) return next(); | |
// Check redis for html | |
var cacheId = 'htmlCache' + app.config.seoVersion + ':' + req.url; | |
app.redis.get(cacheId, function(err, cachedHtml) { | |
if (!err && cachedHtml) return res.send(cachedHtml); | |
// Generate HTML and cache it to redis | |
Zombie.visit(app.config.siteUrl + req.url, { | |
debug: app.config.isDev, | |
runScripts: true, | |
loadCSS: true, | |
silent: true, | |
headers: { bot: true } | |
}, function (e, browser, status) { | |
var html = browser.html(); | |
res.send(html); | |
// Save to redis | |
app.redis.setex(cacheId, (5 * 24 * 60 * 60), html); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment