Skip to content

Commit

Permalink
🐛 fix api.getPostPaths not returning posts
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Fernandez <albertofdzm@gmail.com>
  • Loading branch information
AlbertoFdzM committed Nov 19, 2023
1 parent 16a059c commit cc6371b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
32 changes: 19 additions & 13 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs";
import path from "path";

import grayMatter from "gray-matter";
import { globbySync } from "globby";
import { globby } from "globby";
import stripMarkdown from "strip-markdown";
import { visit } from "unist-util-visit";
import { toString } from "mdast-util-to-string";
Expand All @@ -14,6 +14,8 @@ import PostType from "@/interfaces/post";

const postsDirectoryPath = path.join(process.cwd(), "public/posts");

const MINIMUM_PAGE = 1;

interface Cache {
postPaths: Set<string>;
postPathsBySlug: Map<string, string>;
Expand All @@ -26,15 +28,15 @@ const cache: Cache = {
postSlugsByPath: new Map(),
};

export function getPostPaths(page?: number) {
export async function getPostPaths(page?: number): Promise<string[]> {
let postPaths: string[] = Array.from(cache.postPaths.values());

if (postPaths.length === 0) {
postPaths = globbySync(postsDirectoryPath, {
expandDirectories: {
extensions: ["md"],
},
});
postPaths = await globby(path.join(postsDirectoryPath, "**/*.md"));

if (postPaths.length === 0) {
console.warn(`No posts found in ${postsDirectoryPath}`);
}

postPaths.forEach((postPath) => cache.postPaths.add(postPath));
}
Expand All @@ -49,7 +51,11 @@ export function getPostPaths(page?: number) {
});
}

if (page) {
if (page !== undefined) {
if (page < MINIMUM_PAGE) {
throw new Error(`Invalid page number ${page}`);
}

postPaths = postPaths.slice(
(page - 1) * Number(process.env.NEXT_PUBLIC_POSTS_PER_PAGE),
page * Number(process.env.NEXT_PUBLIC_POSTS_PER_PAGE),
Expand Down Expand Up @@ -160,7 +166,7 @@ export async function getPostBySlug(slug: string) {
}

export async function getPosts(page?: number) {
const postPaths: string[] = getPostPaths(page);
const postPaths: string[] = await getPostPaths(page);

const posts = await Promise.all(
postPaths.map((postPath) => getPostByPath(postPath)),
Expand All @@ -173,12 +179,12 @@ export async function getPosts(page?: number) {
return sortedPosts;
}

export function getTotalPosts(): number {
return getPostPaths().length;
export async function getTotalPosts(): Promise<number> {
return (await getPostPaths()).length;
}

export function getTotalPages() {
const totalPosts = getTotalPosts();
export async function getTotalPages(): Promise<number> {
const totalPosts = await getTotalPosts();

const totalPages: number = Math.ceil(
totalPosts / Number(process.env.NEXT_PUBLIC_POSTS_PER_PAGE),
Expand Down
11 changes: 5 additions & 6 deletions pages/[page].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ interface Props {
totalPages: number;
}

const PaginatedPostsPage: React.JSXElementConstructor<Props> = ({
currentPage,
posts,
totalPages,
}: Props) => {
const PaginatedPostsPage: React.JSXElementConstructor<Props> = (
props: Props,
) => {
const { currentPage, posts = [], totalPages } = props;
const nextPage: number = currentPage + 1;
const previousPage: number = currentPage - 1;

Expand Down Expand Up @@ -73,7 +72,7 @@ export const getStaticProps: GetStaticProps<Props, Params> = async (
) => {
const currentPage: number = Number(context.params?.page);

const totalPages: number = getTotalPages();
const totalPages: number = await getTotalPages();

const allPosts: PostsProp = await getPosts();

Expand Down
7 changes: 3 additions & 4 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ interface Props {
totalPages: number;
}

const IndexPage: React.JSXElementConstructor<Props> = ({
posts,
totalPages,
}: Props) => {
const IndexPage: React.JSXElementConstructor<Props> = (props: Props) => {
const { posts, totalPages } = props;

return (
<Layout>
<PostsList className="mx-auto max-w-sm sm:max-w-full" posts={posts} />
Expand Down

0 comments on commit cc6371b

Please sign in to comment.