Skip to content

Commit baeca09

Browse files
committed
Add slug for blog (#142)
* Add slug for blog * Update slug
1 parent 3e72b86 commit baeca09

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

app/news/[slug]/page.jsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {notFound, redirect} from "next/navigation";
2+
import {news} from "../data.js";
3+
4+
function toKebabCase(str) {
5+
return str.toLowerCase().replace(/ /g, "-");
6+
}
7+
8+
function slugof(news) {
9+
return news.slug || toKebabCase(news.title);
10+
}
11+
12+
export async function generateStaticParams() {
13+
return news.map((news) => ({slug: slugof(news)}));
14+
}
15+
16+
export async function generateMetadata({params}) {
17+
const {slug} = await params;
18+
const blog = news.find((news) => slugof(news) === slug);
19+
if (!blog) notFound();
20+
return {
21+
title: `${blog.title} | Recho`,
22+
};
23+
}
24+
25+
export default async function Page({params}) {
26+
const {slug} = await params;
27+
const blog = news.find((news) => slugof(news) === slug);
28+
if (!blog) notFound();
29+
redirect(blog.link);
30+
}

app/news/data.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const news = [
2+
{
3+
title: "A Lighter Way to Code with Creativity",
4+
publishedAt: "2025-10-01",
5+
summary: "Introducing Recho: a light learning and exploration environment.",
6+
link: "https://medium.com/@subairui/a-lighter-way-to-code-with-creativity-8c0ac739aa6f",
7+
author: "Bairui SU",
8+
image: "a-ligter-way-to-code-with-creativity.png",
9+
slug: "hello-world",
10+
},
11+
];

app/news/page.jsx

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
import {cn} from "../cn.js";
2-
3-
const news = [
4-
{
5-
title: "A Lighter Way to Code with Creativity",
6-
publishedAt: "2025-10-01",
7-
summary: "Introducing Recho: a light learning and exploration environment.",
8-
link: "https://medium.com/@subairui/a-lighter-way-to-code-with-creativity-8c0ac739aa6f",
9-
author: "Bairui SU",
10-
image: "a-ligter-way-to-code-with-creativity.png",
11-
},
12-
];
2+
import {news} from "./data.js";
133

144
export default function News() {
155
return (
-2.82 KB
Loading

0 commit comments

Comments
 (0)