Use actual redirects

This commit is contained in:
Shadowfacts 2019-09-18 13:15:47 -04:00
parent 3965d03971
commit 9be7530b15
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 9 additions and 48 deletions

View File

@ -4,7 +4,6 @@ import css from "./css";
import errors from "./errors"; import errors from "./errors";
import homepage from "./homepage"; import homepage from "./homepage";
import posts from "./posts"; import posts from "./posts";
import redirects from "./redirects";
import rss from "./rss"; import rss from "./rss";
import tutorials from "./tutorials"; import tutorials from "./tutorials";
@ -15,7 +14,6 @@ export = {
errors, errors,
homepage, homepage,
posts, posts,
redirects,
rss, rss,
tutorials tutorials
}; };

View File

@ -1,29 +0,0 @@
import * as metadata from "../metadata";
import { Page } from "../metadata";
import layout from "../layout";
import * as util from "../util";
async function generateRedirect(oldPermalink: string, newPermalink: string) {
const page = await metadata.get("site/redirect.html.ejs");
page.text = util.render(page.text, {
metadata: page.metadata,
newPermalink
}, "site/redirect.html.ejs");
page.text = await layout(page.text, page.metadata, page.metadata.layout!);
if (oldPermalink.endsWith("/")) {
oldPermalink += "index.html";
}
util.write(oldPermalink, page.text);
}
export default async function redirects(posts: Page[]) {
for (const post of posts) {
if (post.metadata.oldPermalink) {
await generateRedirect(post.metadata.oldPermalink, post.metadata.permalink);
}
}
}

View File

@ -20,7 +20,6 @@ async function generate(): Promise<Page[]> {
const posts = await generators.posts(); const posts = await generators.posts();
generators.homepage(posts); generators.homepage(posts);
generators.redirects(posts);
const categories = await generators.categories(posts); const categories = await generators.categories(posts);
await generators.rss(posts, categories, tutorials); await generators.rss(posts, categories, tutorials);
@ -74,6 +73,15 @@ async function generate(): Promise<Page[]> {
app.use(apRouter); app.use(apRouter);
app.use(express.static("out")); app.use(express.static("out"));
// redirect posts with changed permalinks
for (const post of posts) {
if (post.metadata.oldPermalink) {
app.get(post.metadata.oldPermalink, (req, res) => {
res.status(301).redirect(post.metadata.permalink);
});
}
}
app.use((err, req, res, next) => { app.use((err, req, res, next) => {
console.error("Unhandled error:", err); console.error("Unhandled error:", err);
res.status(500).sendFile("500.html", { res.status(500).sendFile("500.html", {

View File

@ -1,16 +0,0 @@
```
metadata.title = "Redirect"
metadata.layout = "default.html.ejs"
```
<script>
window.location = "<%= newPermalink %>";
</script>
<article>
<div class="article-content">
<p>
This page has moved to a new URL. If you are not automatically redirected, click <a href="<%= newPermalink %>">here</a>.
</p>
</div>
</article>