node-ajax-seo
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -0,1 +1,7 @@ | ||
## 1.4.0 (2017-01-28) | ||
Add woorank & some extra Alexa bot + cleaning some code | ||
## ... | ||
## 1.2.3 (2016-01-13) | ||
@@ -2,0 +8,0 @@ |
var path = require('path'); | ||
exports.dealWithAjax = function(siteConfig, req, res, next, cbk){ | ||
//console.log("Receiving request! query: ", req.query, " URL: ", req.url); | ||
exports.dealWithAjax = function(externalConfig, req, res, next, cbk){ | ||
var siteConfig = getConfig(externalConfig); | ||
var fragment = req.query._escaped_fragment_; | ||
var isCrawler = checkCrawler(siteConfig, req); | ||
if(siteConfig.debug) console.log(siteConfig.appPrefix+'isCrawler',isCrawler); | ||
if (isAjaxAllowedRequest(siteConfig, req)) { | ||
if (!isCrawler) { | ||
if(siteConfig.debug) console.log(siteConfig.appPrefix+"sending to ajaxpath: ", siteConfig.ajaxPath); | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.sendFile(siteConfig.ajaxPath); | ||
}else{ | ||
fragment = req._parsedUrl.pathname; | ||
// if(siteConfig.debug) console.log(siteConfig.appPrefix+"Dealing with escaped fragments. Entering with: ",req); | ||
dealWithEscapedFragments(siteConfig, fragment, res, cbk); | ||
} | ||
} else { | ||
next(); | ||
} | ||
}; | ||
function getConfig(externalConfig) { | ||
var siteConfig = externalConfig; | ||
siteConfig.appPrefix = "[node-ajax-seo] "; | ||
@@ -17,3 +40,2 @@ siteConfig.debug = siteConfig.debug || false; | ||
// ajaxPath is compulsory | ||
if(siteConfig.staticPages === undefined){ | ||
@@ -35,43 +57,30 @@ // full equip | ||
} | ||
return siteConfig; | ||
} | ||
//console.log("node-ajax-seo", siteConfig); | ||
function checkCrawler(siteConfig, req) { | ||
var fragment = req.query._escaped_fragment_; | ||
var isCrawler = checkCrawler(req); | ||
var crawlers = ['Alexa', 'ia_archiver', 'FacebookExternalHit', 'FacebookBot', 'Twitterbot', 'Googlebot', 'Bingbot', 'Yahoo', 'Applebot', 'SemrushBot', 'Pinterest', 'Baiduspider', 'LinkedInBot', 'FlipboardProxy', 'FlipboardBot', 'WhatsApp', 'Telegram', 'Slackbot', 'Screaming Frog SEO Spider', 'UptimeRobot', 'Feedly', 'PaperLiBot', 'LoadImpact', 'GTmetrix', 'woobot']; | ||
if(siteConfig.debug) console.log(siteConfig.appPrefix+'isCrawler',isCrawler); | ||
if (isAjaxAllowedRequest(siteConfig, req)) { | ||
//console.log(siteConfig.appPrefix+"asking for Ajax? -> "+ajaxCondition); | ||
if (!isCrawler) { | ||
if(siteConfig.debug) console.log(siteConfig.appPrefix+"sending to ajaxpath: ", siteConfig.ajaxPath); | ||
res.setHeader('Content-Type', 'text/html'); | ||
res.sendFile(siteConfig.ajaxPath); | ||
}else{ | ||
fragment = req._parsedUrl.pathname; | ||
if(siteConfig.debug) console.log(siteConfig.appPrefix+"Dealing with escaped fragments. Entering with: ",req); | ||
dealWithEscapedFragments(siteConfig, fragment, res, cbk); | ||
} | ||
} else { | ||
next(); | ||
} | ||
}; | ||
function checkCrawler(req){ | ||
var fragment = req.query._escaped_fragment_; | ||
var crawlers = ['Alexabot', 'FacebookExternalHit', 'FacebookBot', 'Twitterbot', 'Googlebot', 'Bingbot', 'Yahoo', 'Applebot', 'SemrushBot', 'Pinterest', 'Baiduspider', 'LinkedInBot', 'FlipboardProxy', 'FlipboardBot', 'WhatsApp', 'Telegram', 'Slackbot', 'Screaming Frog SEO Spider', 'UptimeRobot', 'Feedly', 'PaperLiBot', 'LoadImpact', 'GTmetrix']; | ||
// google crawler checking | ||
var isCrawler = (fragment != undefined); | ||
// crawler checking | ||
// We don't put Google here because interferes with Google Img Proxy, with more time we can try to tune better (TODO) | ||
crawlers.forEach(function(c){ | ||
var patt = new RegExp(c,"i"); | ||
isCrawler = isCrawler || (patt.test(req.headers['user-agent'])) ; | ||
}); | ||
// We don't put here Google because interferes with Google Img Proxy, TODO: keep on tunning | ||
if(!isCrawler) { | ||
var ua = req.headers['user-agent']; | ||
var i = 0; | ||
do { | ||
var crawlerUA = crawlers[i]; | ||
var patt = new RegExp(crawlerUA,"i"); | ||
if(patt.test(ua)) { | ||
console.log(siteConfig.appPrefix+"crawler matched [" + crawlerUA + "]"); | ||
isCrawler = true; | ||
} | ||
i++; | ||
} while( i < crawlers.length && !isCrawler ); | ||
} | ||
return isCrawler; | ||
} | ||
function isAjaxAllowedRequest(siteConfig, req){ | ||
function isAjaxAllowedRequest(siteConfig, req) { | ||
var ajaxCondition = false; | ||
@@ -92,3 +101,3 @@ if(siteConfig.nonAjaxCondition instanceof RegExp){ | ||
function dealWithEscapedFragments(siteConfig, fragment, res, cbk) { | ||
console.log(siteConfig.appPrefix+"FRAGMENT! ", fragment); | ||
console.log(siteConfig.appPrefix+"FRAGMENT: ", fragment); | ||
@@ -100,4 +109,2 @@ // If the fragment is empty, serve the default page | ||
// If fragment does not start with '/' prepend it to our fragment | ||
/*if (fragment.charAt(0) !== "/") | ||
fragment = '/' + fragment;*/ | ||
if(fragment.charAt(0) == "/"){ | ||
@@ -107,3 +114,2 @@ fragment = fragment.substring(1); | ||
fragment = fragment.replace(/\//g, siteConfig.staticPages.separator); | ||
//console.log("ESCAPED FRAGMENT, replace result: ", fragment); | ||
@@ -118,3 +124,3 @@ // If fragment does not end with '.html' append it to the fragment | ||
var filePath = siteConfig.staticPages.path; | ||
console.log(siteConfig.appPrefix+"ESCAPED FRAGMENT! trying to serve file: ", path.join(filePath,fragment)); | ||
console.log(siteConfig.appPrefix+"Trying to serve file: ", path.join(filePath,fragment)); | ||
res.setHeader('Content-Type', 'text/html'); | ||
@@ -131,5 +137,5 @@ var options = { | ||
} catch (err) { | ||
console.log(siteConfig.appPrefix+"static page is not found! :( ",err); | ||
console.log(siteConfig.appPrefix+"Static page not found! :( ",err); | ||
res.send(404); | ||
} | ||
} |
{ | ||
"name": "node-ajax-seo", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "It deals with the most popular crawlers (Google, Twitter, Fb, LinkedIn, ...) redirecting them to static files but serving fresh pages to human users.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
10645
118