Permalink
Browse files

Added support for dynamic sitemap generation

  • Loading branch information...
tstrimple committed Jul 14, 2014
1 parent a17a9a1 commit 2bf48540581269ff6bf56492dcd3ba25fc33e4b9
Showing with 28 additions and 0 deletions.
  1. +26 −0 core/server/controllers/frontend.js
  2. +1 −0 core/server/routes/frontend.js
  3. +1 −0 package.json
@@ -10,6 +10,7 @@ var moment = require('moment'),
url = require('url'),
when = require('when'),
Route = require('express').Route,
+ sm = require('sitemap'),
api = require('../api'),
config = require('../config'),
@@ -63,7 +64,32 @@ function handleError(next) {
};
}
+function buildSitemap(posts, done, sitemap) {
+ var sitemap = sitemap || sm.createSitemap ({
+ hostname: config().url,
+ cacheTime: 600000
+ });
+
+ if(posts.length > 0) {
+ var post = posts.shift();
+ sitemap.add({ url: '/' + post.slug + '/' });
+ process.nextTick(buildSitemap.bind(this, posts, done, sitemap));
+ } else {
+ sitemap.toXML(function(xml) {
+ done(xml);
+ });
+ }
+}
+
frontendControllers = {
+ 'sitemap': function(req, res, next) {
+ api.posts.browse({}).then(function(result) {
+ buildSitemap(result.posts, function(sitemap) {
+ res.header('Content-Type', 'application/xml');
+ res.send(sitemap);
+ });
+ });
+ },
'homepage': function (req, res, next) {
// Parse the page number
var pageParam = req.params.page !== undefined ? parseInt(req.params.page, 10) : 1,
@@ -8,6 +8,7 @@ module.exports = function (server) {
var subdir = config().paths.subdir;
// ### Frontend routes
+ server.get('/sitemap.xml', frontend.sitemap);
server.get('/rss/', frontend.rss);
server.get('/rss/:page/', frontend.rss);
server.get('/feed/', function redirect(req, res) {
View
@@ -50,6 +50,7 @@
"rss": "0.2.1",
"semver": "2.2.1",
"showdown": "https://github.com/ErisDS/showdown/archive/v0.3.2-ghost.tar.gz",
+ "sitemap": "^0.7.3",
"sqlite3": "2.2.0",
"unidecode": "0.1.3",
"validator": "3.4.0",

0 comments on commit 2bf4854

Please sign in to comment.