forked from apostrophecms/apostrophe-site-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
577 lines (501 loc) · 17.2 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
var moment = require('moment');
var async = require('async');
var _ = require('@sailshq/lodash');
var fs = require('fs');
var url = require('url');
var defaultLocale = 'default';
module.exports = {
// Cache sitemaps for 1 hour by default. Depending on pagerank
// Google may look at your sitemap somewhere between daily and
// monthly, so don't get your hopes up too far about changing this
cacheLifetime: 60 * 60,
piecesPerBatch: 100,
moogBundle: {
modules: [ 'apostrophe-site-map-custom-pages', 'apostrophe-site-map-pieces' ],
directory: 'lib/modules'
},
afterConstruct: function(self) {
self.apos.tasks.add(self.__meta.name, 'map', self.mapTask);
self.apos.tasks.add(self.__meta.name, 'clear', self.clearTask);
self.addRoutes();
self.enableCache();
},
construct: function(self, options) {
self.caching = true;
self.cacheLifetime = options.cacheLifetime;
self.piecesPerBatch = options.piecesPerBatch;
self.baseUrl = options.baseUrl || self.apos.baseUrl;
self.clearTask = function(apos, argv, callback) {
// Just forget the current sitemaps to make room
// for regeneration on the next request
return self.cache.clear(callback);
};
self.mapTask = function(apos, argv, callback) {
if (argv['update-cache']) {
self.caching = true;
} else {
self.caching = false;
}
if (!self.baseUrl) {
return callback(new Error(
'You must specify the top-level baseUrl option when configuring Apostrophe\n' +
'to use this task. Example: baseUrl: "http://mycompany.com"\n\n' +
'Note there is NO TRAILING SLASH.\n\n' +
'Usually you will only do this in data/local.js, on production.'
));
}
return self.map(callback);
};
self.map = function(callback) {
self.workflow = self.apos.modules['apostrophe-workflow'];
var argv = self.apos.argv;
if (self.caching) {
self.cacheOutput = [];
}
return async.series([
lock,
init,
map,
hreflang,
write,
unlock
], callback);
function lock(callback) {
return self.apos.locks.lock('apostrophe-site-map', callback);
}
function init(callback) {
self.format = argv.format || options.format || 'xml';
self.indent = (typeof(argv.indent) !== 'undefined') ? argv.indent : options.indent;
self.excludeTypes = options.excludeTypes || [];
if (argv['exclude-types']) {
self.excludeTypes = self.excludeTypes.concat(argv['exclude-types'].split(','));
}
self.perLocale = options.perLocale || argv['per-locale'];
// Exception: plaintext sitemaps and sitemap indexes don't go
// together, so we can presume that if they explicitly ask
// for plaintext they are just doing content strategy and we
// should produce a single report
if (self.format === 'text') {
self.perLocale = false;
}
return callback(null);
}
function map(callback) {
self.maps = {};
self.today = moment().format('YYYY-MM-DD');
var locales = [ defaultLocale ];
if (self.workflow) {
locales = _.filter(_.keys(self.workflow.locales), function(locale) {
return !locale.match(/-draft$/) && !self.workflow.locales[locale].private;
});
}
return async.eachSeries(locales, function(locale, callback) {
var req = self.apos.tasks.getAnonReq({ locale: locale });
req.locale = locale;
return async.series([
_.partial(self.getPages, req, locale),
_.partial(self.getPieces, req, locale),
function(callback) {
if (self.custom.length === 1) {
return self.custom(callback);
} else {
return self.custom(req, locale, callback);
}
}
], callback);
}, function(err) {
if (err) {
return callback(err);
}
return callback(null);
});
}
function hreflang(callback) {
var alternativesByGuid = {};
each(function(entry) {
if (!alternativesByGuid[entry.url.workflowGuid]) {
alternativesByGuid[entry.url.workflowGuid] = [];
}
alternativesByGuid[entry.url.workflowGuid].push(entry);
});
each(function(entry) {
if (self.workflow) {
entry.url['xhtml:link'] = [{
_attributes: {
rel: 'alternate',
hreflang: entry.url.workflowLocale,
href: entry.url.loc
}
}];
}
var alternatives = alternativesByGuid[entry.url.workflowGuid];
_.each(alternatives, function(alternative) {
if (alternative === entry) {
return;
}
entry.url['xhtml:link'].push({
_attributes: {
rel: 'alternate',
hreflang: alternative.url.workflowLocale,
href: alternative.url.loc
}
});
});
});
each(function(entry) {
delete entry.url.workflowLocale;
delete entry.url.workflowGuid;
}, true);
return setImmediate(callback);
function each(iterator, ignoreWorkflow) {
_.each(self.maps, function(map) {
_.each(map, function(entry) {
if (typeof(entry) !== 'object') {
return;
}
if (!entry.url.workflowGuid && !ignoreWorkflow) {
return;
}
iterator(entry);
});
});
}
}
function write(callback) {
return self.writeSitemap(callback);
}
function unlock(callback) {
return self.apos.locks.unlock('apostrophe-site-map', callback);
}
};
self.getPages = function(req, locale, callback) {
return self.apos.pages.find(req).areas(false).joins(false).sort({ level: 1, rank: 1 }).toArray(function(err, pages) {
if (err) {
return callback(err);
}
_.each(pages, self.output);
return callback(null);
});
};
self.getPieces = function(req, locale, callback) {
var modules = _.filter(self.apos.modules, function(module, name) {
return _.find(module.__meta.chain, function(entry) {
return entry.name === 'apostrophe-pieces';
});
});
return async.eachSeries(modules, function(module, callback) {
if (_.includes(self.excludeTypes, module.name)) {
return setImmediate(callback);
}
// Paginate through 100 (by default) at a time to
// avoid slamming memory
var done = false;
var skip = 0;
return async.whilst(
function() { return !done; },
function(callback) {
return self.findPieces(req, module).skip(skip).limit(self.piecesPerBatch).toArray(function(err, pieces) {
_.each(pieces, function(piece) {
if (!piece._url) {
// This one has no page to be viewed on
return;
}
// Results in a reasonable priority relative
// to regular pages
piece.level = 3;
// Future events are interesting,
// past events are boring
if (piece.startDate) {
if (piece.startDate > self.today) {
piece.level--;
} else {
piece.level++;
}
}
self.output(piece);
});
if (!pieces.length) {
done = true;
} else {
skip += pieces.length;
}
return callback(null);
});
}, callback);
}, callback);
};
self.writeSitemap = function(callback) {
if (!self.perLocale) {
// Simple single-file sitemap
self.file = self.caching ? 'sitemap.xml' : (self.apos.argv.file || '/dev/stdout');
var map = _.map(_.keys(self.maps), function(locale) {
return _.map(self.maps[locale], self.stringify).join('\n');
}).join('\n');
self.writeMap(self.file, map);
} else {
// They should be broken down by host,
// in which case we automatically place them
// in public/sitemaps in a certain naming pattern
self.ensureDir('sitemaps');
_.each(self.maps, function(map, key) {
var extension = (self.format === 'xml') ? 'xml' : 'txt';
map = _.map(map, self.stringify).join('\n');
self.writeMap('sitemaps/' + key + '.' + extension, map);
});
self.writeIndex();
}
if (self.caching) {
return self.writeToCache(callback);
}
return callback(null);
};
// If `value` is not an object, it is returned as-is,
// or with < & > escaped if `self.format` is `xml`.
//
// If it is an object, it is converted to XML elements,
// one for each property; they may have sub-elements if
// the properties contain objects. The _attributes
// property is converted to attributes. Array
// properties are converted to a series of elements.
//
// TODO: this is clearly yak-shaving, but the data format
// is nice. See if there's another library that takes the same
// or substantially the same format.
self.stringify = function(value) {
if (Array.isArray(value) && (self.format !== 'xml')) {
return value.join('');
}
if (typeof(value) !== 'object') {
if (self.format === 'xml') {
return self.apos.utils.escapeHtml(value);
}
return value;
}
var xml = '';
_.each(value, function(v, k) {
if (k === '_attributes') {
return;
}
if (Array.isArray(v)) {
_.each(v, function(el) {
element(k, el);
});
} else {
element(k, v);
}
function element(k, v) {
xml += '<' + k;
if (v && v._attributes) {
_.each(v._attributes, function(av, a) {
xml += ' ' + a + '="' + self.apos.utils.escapeHtml(av) + '"';
});
}
xml += '>';
xml += self.stringify(v || '');
xml += '</' + k + '>\n';
}
});
return xml;
};
self.ensureDir = function(dir) {
if (!self.caching) {
dir = self.apos.rootDir + '/public/' + dir;
try {
fs.mkdirSync(dir);
} catch (e) {
// exists
}
}
};
self.writeIndex = function() {
var now = new Date();
if (!self.baseUrl) {
throw new Error(
'You must specify the top-level baseUrl option when configuring Apostrophe\n' +
'to use sitemap indexes. Example: baseUrl: "http://mycompany.com"\n\n' +
'Note there is NO TRAILING SLASH.\n\n' +
'Usually you will override this in data/local.js, on production.'
);
}
self.writeFile('sitemaps/index.xml',
'<?xml version="1.0" encoding="UTF-8"?>\n' +
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' +
' xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' +
_.map(_.keys(self.maps), function(key) {
var map = self.maps[key];
var sitemap = ' <sitemap>\n' +
' <loc>' + self.baseUrl + self.apos.prefix + '/sitemaps/' + key + '.xml'
+ '</loc>\n' +
' <lastmod>' + now.toISOString() + '</lastmod>\n' +
' </sitemap>\n';
return sitemap;
}).join('') +
'</sitemapindex>\n'
);
};
self.writeMap = function(file, map) {
if (self.format === 'xml') {
self.writeXmlMap(file, map);
} else {
self.writeFile(file, map);
}
};
self.writeXmlMap = function(file, map) {
self.writeFile(file,
'<?xml version="1.0" encoding="UTF-8"?>\n' +
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' +
' xmlns:xhtml="http://www.w3.org/1999/xhtml">\n' +
map +
'</urlset>\n'
);
};
self.writeFile = function(filename, s) {
if (!self.caching) {
filename = require('path').resolve(self.apos.rootDir + '/public', filename);
if (filename === '/dev/stdout') {
// Strange bug on MacOS when using writeFileSync with /dev/stdout
fs.writeSync(1, s);
} else {
fs.writeFileSync(filename, s);
}
} else {
self.cacheOutput.push({
filename: filename,
data: s,
createdAt: new Date()
});
}
};
self.writeToCache = function(callback) {
return async.series([
clear,
insert
], callback);
function clear(callback) {
return self.cache.clear(callback);
}
function insert(callback) {
return async.eachSeries(self.cacheOutput, function(doc, callback) {
return self.cache.set(doc.filename, doc, self.cacheLifetime, callback);
}, callback);
}
};
// Override to do more. You can invoke `self.output(doc)`
// from here as many times as you like.
self.custom = function(req, locale, callback) {
return callback(null);
};
self.findPieces = function(req, module) {
return module.find(req).published(true).joins(false).areas(false);
};
// Output the sitemap entry for the given doc, including its children if any.
// The entry is buffered for output as part of the map for the appropriate
// locale. If the workflow module is not in use they all accumulate together
// for a "default" locale. Content not subject to workflow is grouped with
// the "default" locale. If workflow is active and the locale is not configured
// or is marked private, the output is discarded.
self.output = function(page) {
var locale = page.workflowLocale || defaultLocale;
if (self.workflow) {
if (!self.workflow.locales[locale]) {
return;
}
if (self.workflow.locales[locale].private) {
return;
}
}
if (!_.includes(self.excludeTypes, page.type)) {
var url;
if (self.format === 'text') {
if (self.indent) {
var i;
for (i = 0; (i < page.level); i++) {
self.write(locale, ' ');
}
self.write(locale, page._url + '\n');
}
} else {
url = page._url;
var priority = (page.level < 10) ? (1.0 - page.level / 10) : 0.1;
if (typeof (page.siteMapPriority) === 'number') {
priority = page.siteMapPriority;
}
self.write(locale, {
url: {
priority: priority,
changefreq: 'daily',
loc: self.baseUrl + self.apos.prefix + url,
workflowGuid: page.workflowGuid,
workflowLocale: locale
}
});
}
}
};
// Append `s` to an array set aside for the map entries
// for the host `locale`.
self.write = function(locale, s) {
self.maps[locale] = self.maps[locale] || [];
self.maps[locale].push(s);
};
self.addRoutes = function() {
// Deliver from our tiny little fake cache filesystem
self.apos.app.get('/sitemap.xml', function(req, res) {
return self.sendCache(res, 'sitemap.xml');
});
self.apos.app.get('/sitemaps/*', function(req, res) {
return self.sendCache(res, 'sitemaps/' + req.params[0]);
});
};
self.sendCache = function(res, path) {
return self.cache.get(path, function(err, file) {
if (err) {
return fail(err);
}
if (!file) {
// If anything else exists in our little filesystem, this
// should be a 404 (think of a URL like /sitemap/madeupstuff).
// Otherwise it just means the
// cache has expired or has never been populated.
//
// Check for the sitemap index or, if we're not
// running in that mode, check for sitemap.xml
//
// Without this check every 404 would cause a lot of work to be done.
return self.cache.get(self.perLocale ? 'sitemaps/index.xml' : 'sitemap.xml', function(err, exists) {
if (err) {
return fail(err);
}
if (exists) {
return notFound();
}
return self.cacheAndRetry(res, path);
});
}
return res.contentType('text/xml').send(file.data);
});
function notFound() {
return res.status(404).send('not found');
}
function fail(err) {
console.error(err);
return res.status(500).send('error');
}
};
self.cacheAndRetry = function(res, path) {
return self.map(function(err) {
if (err) {
return fail(err);
}
return self.sendCache(res, path);
});
function fail(err) {
console.error(err);
return res.status(500).send('error');
}
};
self.enableCache = function() {
self.cache = self.apos.caches.get('apostrophe-sitemap');
};
}
};