From 78b57dc4ad19b9cd4acc1c69c0b791a3106013cf Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 6 Nov 2022 21:46:56 +0100 Subject: [PATCH] with cache --- app/posts.tsx | 4 +++- lib/getPosts.tsx | 36 ++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) 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); + }, + ); + }); +};