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]} />
|
<ReactMarkdown children={p.message} remarkPlugins={[remarkGfm]} />
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<small>{new Date(p.time * 1000).toLocaleString()}</small>
|
<small className="mt-2">
|
||||||
|
{new Date(p.time * 1000).toLocaleString()}
|
||||||
|
</small>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
</>
|
</>
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { exec } from 'child_process';
|
import { exec } from 'child_process';
|
||||||
|
import { cache } from 'react';
|
||||||
|
|
||||||
export type Post = {
|
export type Post = {
|
||||||
title: string;
|
title: string;
|
||||||
@ -10,19 +11,9 @@ export type Post = {
|
|||||||
time: number;
|
time: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
const posts: Post[] = [
|
export const getPosts = cache(async (): Promise<Post[]> => {
|
||||||
{
|
await updatePosts();
|
||||||
author: 'kjuulh',
|
|
||||||
post: 'some post\n',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
author: 'kjuulh',
|
|
||||||
post: `some other post with
|
|
||||||
some more text`,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
export const getPosts = async (): Promise<Post[]> => {
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
exec(`git-log --path ${process.env.POSTS_DIR}`, (error, stdout, stderr) => {
|
exec(`git-log --path ${process.env.POSTS_DIR}`, (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
@ -36,6 +27,23 @@ export const getPosts = async (): Promise<Post[]> => {
|
|||||||
resolve(posts);
|
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