Skip to content

Commit

Permalink
publishing/rendering express routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jonwinton committed Jun 12, 2018
1 parent ce3a017 commit 249b4d6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ function getExpressRoutePrefix(site) {
function renderUri(uri, req, res, hrStart) {
hrStart = hrStart || process.hrtime(); // Check if we actually have a start time

return db.get(uri).then(function (result) {

return db.get(uri).then(JSON.parse).then(function ({ value: result }) {
const route = _.find(uriRoutes, function (item) {
return result.match(item.when);
});
Expand Down
9 changes: 7 additions & 2 deletions lib/services/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,13 @@ function makeMongoBatch(ops) {
let { key, value} = ops[i],
type = key.match(/\/_(.+?)\//)[1];

value = JSON.parse(value);
value._id = key;
try {
value = JSON.parse(value);
value._id = key;
} catch (e) {
value = { _id: key, value };
}

mongoOps[type].push(value);
}

Expand Down
7 changes: 5 additions & 2 deletions lib/services/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ const _ = require('lodash'),
function storeUrlHistory(url, uri, data) {
// get the published page (if it exists) and update the urlHistory array
return db.get(uri)
.then(JSON.parse)
.then(d => {
// When using Mongo a non-existent key returns null
return d !== 'null' ? JSON.parse(d) : {};
})
.catch(function () {
// if this is the first time we're publishing, db.get() won't find
// the published page data. thus, return empty object so we can add the urlHistory
return {};
})
.then(function (publishedPageData) {
data.urlHistory = publishedPageData.urlHistory || [];
data.urlHistory = publishedPageData && publishedPageData.urlHistory || [];

if (_.last(data.urlHistory) !== url) {
// only add urls if they've changed
Expand Down

0 comments on commit 249b4d6

Please sign in to comment.