From 249b4d663f80d0204b8724c57b6a7c8828a8cc9d Mon Sep 17 00:00:00 2001 From: Jon Winton Date: Tue, 12 Jun 2018 14:19:54 -0400 Subject: [PATCH] publishing/rendering express routes --- lib/render.js | 3 ++- lib/services/db.js | 9 +++++++-- lib/services/publish.js | 7 +++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/render.js b/lib/render.js index 0905c8f9..29fa8222 100644 --- a/lib/render.js +++ b/lib/render.js @@ -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); }); diff --git a/lib/services/db.js b/lib/services/db.js index fd202ce7..c0763068 100644 --- a/lib/services/db.js +++ b/lib/services/db.js @@ -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); } diff --git a/lib/services/publish.js b/lib/services/publish.js index 6b2f067a..5f325298 100644 --- a/lib/services/publish.js +++ b/lib/services/publish.js @@ -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