Skip to content

Commit

Permalink
Fix to return correct data
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina committed Oct 9, 2023
1 parent 831d78a commit 298121b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/app/blog/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export async function generateMetadata({ params }: { params: BlogInfo }): Promis
return { title };
}

export async function generateStaticParams(): Promise<BlogInfos> {
return getAllBlogInfos();
}

export default async function Page({ params }: { params?: BlogInfo }) {
if (params == null) {
throw new Error("invalid params");
Expand Down
6 changes: 1 addition & 5 deletions src/app/blog/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { BlogInfos, getAllBlogInfos, getSortedBlogMetadata } from "../lib/blog-fetch";
import { Blog } from "./blog";

export async function generateStaticParams(): Promise<BlogInfos> {
return getAllBlogInfos();
}
import { getSortedBlogMetadata } from "../lib/blog-fetch";

export default async function Page() {
const blogs = await getSortedBlogMetadata();
Expand Down
14 changes: 5 additions & 9 deletions src/app/lib/blog-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export type BlogInfo = {
id: string;
};

export type BlogInfos = { params: BlogInfo }[];
export type BlogInfos = BlogInfo[];

const markdownPattern = /\.md$/;

Expand All @@ -100,11 +100,7 @@ export async function getAllBlogInfos(): Promise<BlogInfos> {
console.log(`Reading all blog infos from ${postsDirectory}`);
const fileNames = await readdir(postsDirectory);

return Promise.all(
fileNames.map(async (fileName: string) => ({
params: await blogInfoFromFileName(fileName),
})),
);
return Promise.all(fileNames.map((fileName: string) => blogInfoFromFileName(fileName)));
}

export async function getBlogFromId(id: string): Promise<Blog> {
Expand All @@ -120,12 +116,12 @@ export async function getBlogFromId(id: string): Promise<Blog> {
}

const blogInfos = await getAllBlogInfos();
const idx = blogInfos.findIndex((e) => e.params.id === id);
const idx = blogInfos.findIndex((e) => e.id === id);

return {
content,
...data,
prevId: blogInfos[idx - 1]?.params.id ?? "",
nextId: blogInfos[idx + 1]?.params.id ?? "",
prevId: blogInfos[idx - 1]?.id ?? "",
nextId: blogInfos[idx + 1]?.id ?? "",
};
}

0 comments on commit 298121b

Please sign in to comment.