with cache
This commit is contained in:
parent
bce0931df9
commit
78b57dc4ad
@ -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>
|
||||
))}
|
||||
</>
|
||||
|
@ -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);
|
||||
},
|
||||
);
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user