diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/blog/blog.js | 22 | ||||
-rw-r--r-- | modules/blog/template.js | 8 |
2 files changed, 22 insertions, 8 deletions
diff --git a/modules/blog/blog.js b/modules/blog/blog.js index 748bcde..a866003 100644 --- a/modules/blog/blog.js +++ b/modules/blog/blog.js @@ -15,6 +15,10 @@ let templateCache = new Map(); // callbacks that take (http status code, rendered html [= null if status != 200]) let renderWatchers = new Map(); +// Number of times the repo was updated. If this number does not match the +// value before you did an async operation, your state may be outdated. +let repoUpdateCount = 0; + function triggerRenderWatchers(path, statusCode, rendered) { const list = renderWatchers.get(path); if (list !== undefined) { @@ -86,6 +90,7 @@ function updateRepo() { // Reset cache _after_ the commands succeeded; if anything failed, we // might at least still have stale cache data templateCache = new Map(); + repoUpdateCount++; } catch (e) { console.error("Cannot update blog git repo!"); console.error(e); @@ -129,9 +134,15 @@ module.exports = (app, io, _moddir) => { const path = req.path .slice(6) + .replace(/^\/+/, "") .replace(/\/[\/]*/g, "/") .replace(/\/\.+/g, "/") - .replace(/\.html$/, ""); + .replace(/\.html$/i, ""); + + if (req.path != "/blog/" + path) { + res.redirect("/blog/" + path); + return; + } const fromCache = templateCache.get(path); @@ -147,16 +158,19 @@ module.exports = (app, io, _moddir) => { } else { // Indicate that this path is currently being rendered templateCache.set(path, null); + const prevUpdateCount = repoUpdateCount; - generateTemplate(repodir, path ? path + ".html" : undefined) + generateTemplate(repodir, path + ".html") .then(rendered => { - templateCache.set(path, rendered); + // Don't set the cache if we rendered outdated content + if (repoUpdateCount == prevUpdateCount) + templateCache.set(path, rendered); triggerRenderWatchers(path, 200, rendered); res.send(rendered); }) .catch(err => { if (err.code && err.code == "ENOENT") { - triggerRenderWatchers(path, 400, null); + triggerRenderWatchers(path, 404, null); res.sendStatus(404); } else { triggerRenderWatchers(path, 500, null); diff --git a/modules/blog/template.js b/modules/blog/template.js index 38ce72e..32d7bb3 100644 --- a/modules/blog/template.js +++ b/modules/blog/template.js @@ -19,6 +19,7 @@ async function recursiveTree(dir) { return res; } +// path: url prefix function generateTree(tree, path) { let out = ""; @@ -45,11 +46,10 @@ async function template(repodir, contentPath) { let html = await fs.readFile(repodir + "/$template.html", { encoding: "utf-8" }); + const content = await fs.readFile(repodir + "/" + contentPath, { encoding: "utf-8" }); + html = html.replace("<!-- REPLACE TREE -->", generateTree(tree, pathRoot)); - if (contentPath) { - const content = await fs.readFile(repodir + "/" + contentPath, { encoding: "utf-8" }); - html = html.replace("<!-- REPLACE CONTENT -->", content); - } + html = html.replace("<!-- REPLACE CONTENT -->", content); return html; } |