mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-15 16:51:37 +00:00
add cache for fetch post functions
This commit is contained in:
@ -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' });
|
||||
};
|
||||
});
|
||||
|
@ -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.
|
||||
|
Reference in New Issue
Block a user