-
Notifications
You must be signed in to change notification settings - Fork 61
/
seo.js
119 lines (86 loc) · 2.49 KB
/
seo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* SEO MiddleWare
*
* Responsible of checking if a request is from a Search Engine
* If so, returns a Cached page if exists, otherwise goes on and
* marks the url as pending for being cached
*/
var prerender = require('prerender-node');
var mongo = require('mongodb');
var MongoClient = require('mongodb').MongoClient;
var config = require('config');
var db;
module.exports = function(app) {
var prCfg = config.prerender;
var db_uri = prCfg.db || 'mongodb://localhost/prerender'
MongoClient.connect(db_uri, function(err, _db) {
if (err) {
console.log(err);
return;
}
db = _db
});
return function(req, res, next){
if (!db){
console.log('no database connection for prerender found.');
return next();
}
if(!prerender.shouldShowPrerenderedPage(req)) {
// is not a Search Engine -> GO ON
return next();
}
// IS A SEARCH ENGINE ------------------------------------------
var url = '/' + req.protocol + "://" + req.get('host') + req.url;
getCachedPage(url, function(err, page){
if (!page){
// Not cached page, store as pending and go on
setCachedPage(url);
return next();
}
if (!page.pending){
// Exists and is not pending
if (page.value && page.value.trim().length > 0){
// has a valid value
return res.send(200, removeScripts(page.value) );
}
// for some reason the cache is invalid, re build it
reCachePage(url);
}
return next();
});
};
};
function getCachedPage(url, done){
db.collection('pages', function(err, collection) {
collection.findOne({ key: url }, done);
});
}
function setCachedPage(url){
db.collection('pages', function(err, collection) {
if (err){ return console.log(err); }
collection.insert({
key: url,
value: '',
created: new Date(),
pending: true
}, { w: 0 });
});
}
function reCachePage(url){
db.collection('pages', function(err, collection) {
if (err){ return console.log(err); }
collection.remove({ key: url }, function(err){
if (err){ return console.log(err); }
setCachedPage(url);
});
});
}
function removeScripts(html){
var matches = html.toString().match(/<script(?:.*?)>(?:[\S\s]*?)<\/script>/gi);
for (var i = 0; matches && i < matches.length; i++) {
if(matches[i].indexOf('application/ld+json') === -1) {
html = html.toString().replace(matches[i], '');
}
}
return html;
}