diff --git a/app/posts.tsx b/app/posts.tsx index 6192675..f5e23e6 100644 --- a/app/posts.tsx +++ b/app/posts.tsx @@ -20,7 +20,9 @@ const Posts: FC = ({ posts }) => { )} - {new Date(p.time * 1000).toLocaleString()} + + {new Date(p.time * 1000).toLocaleString()} + ))} diff --git a/lib/getPosts.tsx b/lib/getPosts.tsx index e18a657..cf215c4 100644 --- a/lib/getPosts.tsx +++ b/lib/getPosts.tsx @@ -1,4 +1,5 @@ import { exec } from 'child_process'; +import { cache } from 'react'; export type Post = { title: string; @@ -10,19 +11,9 @@ export type Post = { time: number; }; -const posts: Post[] = [ - { - author: 'kjuulh', - post: 'some post\n', - }, - { - author: 'kjuulh', - post: `some other post with - some more text`, - }, -]; +export const getPosts = cache(async (): Promise => { + await updatePosts(); -export const getPosts = async (): Promise => { return new Promise((resolve, reject) => { exec(`git-log --path ${process.env.POSTS_DIR}`, (error, stdout, stderr) => { if (error) { @@ -36,6 +27,23 @@ export const getPosts = async (): Promise => { resolve(posts); }); }); -}; +}); -export const revalidate = 5; +const updatePosts = async () => { + return new Promise((resolve, reject) => { + exec( + `(cd ${process.env.POSTS_DIR}; git pull --rebase)`, + (error, stdout, stderr) => { + if (error) { + console.warn(error); + reject( + 'could not update posts stderr: ' + stderr + 'stdout: ' + stdout, + ); + return; + } + + resolve(true); + }, + ); + }); +};