Skip to content

Commit

Permalink
feat: revalidate cache on essays
Browse files Browse the repository at this point in the history
  • Loading branch information
iamjoshua committed Sep 22, 2023
1 parent 34536b3 commit fd403e8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 32 deletions.
5 changes: 2 additions & 3 deletions app/on/[title]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand All @@ -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 (
<div className="p-20 w-full mx-auto w-fit">
<ReactMarkdown className="prose prose-base max-w-[900px] prose-headings:font-thin prose-h1:text-3xl">
Expand Down
50 changes: 23 additions & 27 deletions app/on/getWritings.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
const cache: Record<string, any> = {};
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<any[]> {
async function fetchMdFiles(path: string, tag: string): Promise<any[]> {
try {
const response = await fetchRepo(path);
const response = await fetchRepo(path, tag);
if (!response.ok) {
throw new Error(`GitHub API responded with ${response.status}`);
}
Expand Down Expand Up @@ -51,19 +38,29 @@ function extractTitles(fileNames: string[]): string[] {
return fileNames.map((name) => name.replaceAll("-", " ").replace(".md", ""));
}

async function fetchFileContents(files: any[]): Promise<Map<string, string>> {
async function fetchFileContents(
files: any[],
tag: string,
): Promise<Map<string, string>> {
const contentMap = new Map<string, string>();

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<string> {
const response = await fetch(url);
export async function fetchFileContent(
url: string,
tag: string,
): Promise<string> {
const response = await fetch(url, {
cache: "force-cache",
next: { tags: [tag] },
});

if (response.ok) {
const content = await response.text();
return content;
Expand All @@ -72,20 +69,19 @@ export async function fetchFileContent(url: string): Promise<string> {
}
}

async function fetchRepo(path: string): Promise<any> {
async function fetchRepo(path: string, tag: string): Promise<any> {
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;
}

Expand Down
3 changes: 1 addition & 2 deletions app/on/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="p-5">
Expand Down

1 comment on commit fd403e8

@vercel
Copy link

@vercel vercel bot commented on fd403e8 Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.