diff --git a/app/on/[title]/page.tsx b/app/on/[title]/page.tsx index 1abb575..a5f1f67 100644 --- a/app/on/[title]/page.tsx +++ b/app/on/[title]/page.tsx @@ -2,7 +2,7 @@ import { ReactMarkdown } from "react-markdown/lib/react-markdown"; import { fetchMdFile, getEssayTitles, parseMdFile } from "../getWritings"; export async function generateStaticParams() { - const titles = await getEssayTitles("essays"); + const titles = await getEssayTitles("essays/", "essays"); console.log("generate?", titles); return titles.map((title: string) => ({ @@ -16,10 +16,9 @@ export default async function Essay({ params: { title: string; content: string; magic: string }; }) { const path = `essays/${params.title.replaceAll(" ", "-")}.md`; - const content = await fetchMdFile(path); + const content = await fetchMdFile(path, "essays"); console.log("Esasy:", path, params.magic); const file = await parseMdFile(content); - console.log("wow", file); return (
diff --git a/app/on/getWritings.ts b/app/on/getWritings.ts index 8917fb2..aeba83a 100644 --- a/app/on/getWritings.ts +++ b/app/on/getWritings.ts @@ -1,28 +1,15 @@ -const cache: Record = {}; +const BASE_URL = "https://api.github.com"; -export async function getEssayTitles(path: string) { - const essays = await fetchMdFiles(path); +export async function getEssayTitles(path: string, tag: string) { + const essays = await fetchMdFiles(path, tag); const fileNames = extractFileNames(essays); const titles = extractTitles(fileNames); return titles; } -export async function getWritings(path: string) { - if (cache?.titles) return cache; - console.log("no cache"); - const mdFiles = await fetchMdFiles(path); - const titles = extractFileNames(mdFiles); - const contents: string[] = []; /// await fetchFileContents(mdFiles); - cache.titles = titles; - cache.contents = contents; - return { titles, contents }; -} - -const BASE_URL = "https://api.github.com"; - -async function fetchMdFiles(path: string): Promise { +async function fetchMdFiles(path: string, tag: string): Promise { try { - const response = await fetchRepo(path); + const response = await fetchRepo(path, tag); if (!response.ok) { throw new Error(`GitHub API responded with ${response.status}`); } @@ -51,19 +38,29 @@ function extractTitles(fileNames: string[]): string[] { return fileNames.map((name) => name.replaceAll("-", " ").replace(".md", "")); } -async function fetchFileContents(files: any[]): Promise> { +async function fetchFileContents( + files: any[], + tag: string, +): Promise> { const contentMap = new Map(); for (const file of files) { - const content = await fetchFileContent(file); + const content = await fetchFileContent(file, tag); contentMap.set(file.name, content); } return contentMap; } -export async function fetchFileContent(url: string): Promise { - const response = await fetch(url); +export async function fetchFileContent( + url: string, + tag: string, +): Promise { + const response = await fetch(url, { + cache: "force-cache", + next: { tags: [tag] }, + }); + if (response.ok) { const content = await response.text(); return content; @@ -72,20 +69,19 @@ export async function fetchFileContent(url: string): Promise { } } -async function fetchRepo(path: string): Promise { +async function fetchRepo(path: string, tag: string): Promise { const url = `${BASE_URL}/repos/iamjoshua/writings/contents/${path}`; const response = await fetch(url, { cache: "force-cache", - next: { tags: ["essays"] }, + next: { tags: [tag] }, }); return response; } -export async function fetchMdFile(path: string) { - const response = await fetchRepo(path); +export async function fetchMdFile(path: string, tag: string) { + const response = await fetchRepo(path, tag); const data = await response.json(); - console.log(data); return data; } diff --git a/app/on/page.tsx b/app/on/page.tsx index 5b7f9fe..ecb9975 100644 --- a/app/on/page.tsx +++ b/app/on/page.tsx @@ -3,8 +3,7 @@ import ReactMarkdown from "react-markdown"; import { getEssayTitles } from "./getWritings"; export default async function On() { - const titles = await getEssayTitles("essays"); - console.log("inside On:", titles); + const titles = await getEssayTitles("essays/", "essays"); return (