add cache for fetch post functions

This commit is contained in:
DefectingCat
2023-11-10 14:58:11 +08:00
parent f19c215386
commit 2341e7a3f9
2 changed files with 6 additions and 4 deletions

View File

@ -3,6 +3,7 @@ import path from 'path';
import matter from 'gray-matter';
import { MyMatters, Post } from 'types';
import { sortByDate } from 'lib/utils';
import { cache } from 'react';
export const dataPath = 'content/posts';
@ -74,7 +75,7 @@ export const allPostsPath = async () => {
* @param slug
* @returns
*/
export const readSinglePost = async (slug: string) => {
export const readSinglePost = cache(async (slug: string) => {
const filename = path.join(`${dataPath}/${slug}.mdx`);
return await fs.readFile(filename, { encoding: 'utf-8' });
};
});

View File

@ -1,3 +1,4 @@
import { cache } from 'react';
import * as THREE from 'three';
export const sortByDate = (
@ -47,7 +48,7 @@ export type SingleToc = {
* @param source
* @returns
*/
export const generateToc = (source: string) => {
export const generateToc = cache((source: string) => {
const regex = /^#{2,3}(?!#)(.*)/gm;
let lastH2: SingleToc | null = null;
@ -86,7 +87,7 @@ export const generateToc = (source: string) => {
});
return toc;
};
});
/**
* Get mouse or touch position on screen.