Use actual redirects
This commit is contained in:
parent
3965d03971
commit
9be7530b15
|
@ -4,7 +4,6 @@ import css from "./css";
|
|||
import errors from "./errors";
|
||||
import homepage from "./homepage";
|
||||
import posts from "./posts";
|
||||
import redirects from "./redirects";
|
||||
import rss from "./rss";
|
||||
import tutorials from "./tutorials";
|
||||
|
||||
|
@ -15,7 +14,6 @@ export = {
|
|||
errors,
|
||||
homepage,
|
||||
posts,
|
||||
redirects,
|
||||
rss,
|
||||
tutorials
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
10
lib/index.ts
10
lib/index.ts
|
@ -20,7 +20,6 @@ async function generate(): Promise<Page[]> {
|
|||
|
||||
const posts = await generators.posts();
|
||||
generators.homepage(posts);
|
||||
generators.redirects(posts);
|
||||
const categories = await generators.categories(posts);
|
||||
await generators.rss(posts, categories, tutorials);
|
||||
|
||||
|
@ -74,6 +73,15 @@ async function generate(): Promise<Page[]> {
|
|||
app.use(apRouter);
|
||||
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) => {
|
||||
console.error("Unhandled error:", err);
|
||||
res.status(500).sendFile("500.html", {
|
||||
|
|
|
@ -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>
|
Loading…
Reference in New Issue