From 28b56b0d39831e6ed971d2d62e9505e25f5db95e Mon Sep 17 00:00:00 2001 From: Jeremy Kauffman Date: Sun, 30 Sep 2018 22:06:35 -0400 Subject: [PATCH] "improve" 404/301 --- app/modules/{redirect.js => redirectOr404.js} | 0 app/views/api.js | 3 +++ app/views/redirect.js | 12 ++---------- 3 files changed, 5 insertions(+), 10 deletions(-) rename app/modules/{redirect.js => redirectOr404.js} (100%) diff --git a/app/modules/redirect.js b/app/modules/redirectOr404.js similarity index 100% rename from app/modules/redirect.js rename to app/modules/redirectOr404.js diff --git a/app/views/api.js b/app/views/api.js index 02c4385..eec4d23 100644 --- a/app/views/api.js +++ b/app/views/api.js @@ -6,6 +6,7 @@ import asyncHtml from "choo-async/html"; import dedent from "dedent"; +import redirectOr404 from "../modules/redirectOr404"; const fetch = require("make-fetch-happen").defaults({ cacheManager: "./cache" }); @@ -59,6 +60,8 @@ module.exports = exports = state => parseApiFile(state.params.wildcard).then(res `; +}).catch(() => { + redirectOr404(state.href); }); diff --git a/app/views/redirect.js b/app/views/redirect.js index 4e921f3..bbd22f2 100644 --- a/app/views/redirect.js +++ b/app/views/redirect.js @@ -11,9 +11,7 @@ import fs from "graceful-fs"; import html from "choo/html"; import path from "path"; import { require as local } from "app-root-path"; -import redirects from "../data/redirects.json"; -import redirect from "../modules/redirect"; -import Page404 from "./404.js"; +import redirectOr404 from "../modules/redirectOr404"; import raw from "choo/html/raw"; // V A R I A B L E S @@ -44,17 +42,11 @@ const md = require("markdown-it")({ module.exports = exports = (state, emit) => { // eslint-disable-line let path; - if (state.route === "resources/*") path = `resources/${state.params.wildcard}`; else path = state.params.wildcard; if (!fs.existsSync(`./documents/${path}.md`)) { - const redirectUrl = redirects[path] || redirects["/" + path]; - if (redirectUrl) { - redirect(redirectUrl); - } else { - return Page404(); - } + return redirectOr404(state.href); } const markdownFile = fs.readFileSync(`./documents/${path}.md`, "utf-8");