diff --git a/lib/posts.ts b/lib/posts.ts index da1f3fe..2660d86 100644 --- a/lib/posts.ts +++ b/lib/posts.ts @@ -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' }); -}; +}); diff --git a/lib/utils/index.ts b/lib/utils/index.ts index 0a2c95b..bc4da87 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -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.