with cache

This commit is contained in:
Kasper Juul Hermansen 2022-11-06 21:46:56 +01:00
parent bce0931df9
commit 78b57dc4ad
Signed by: kjuulh
GPG Key ID: 0F95C140730F2F23
2 changed files with 25 additions and 15 deletions

View File

@ -20,7 +20,9 @@ const Posts: FC<PostProps> = ({ posts }) => {
<ReactMarkdown children={p.message} remarkPlugins={[remarkGfm]} />
)}
</div>
<small>{new Date(p.time * 1000).toLocaleString()}</small>
<small className="mt-2">
{new Date(p.time * 1000).toLocaleString()}
</small>
</div>
))}
</>

View File

@ -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<Post[]> => {
await updatePosts();
export const getPosts = async (): Promise<Post[]> => {
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<Post[]> => {
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);
},
);
});
};