mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-16 01:01:38 +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 matter from 'gray-matter';
|
||||||
import { MyMatters, Post } from 'types';
|
import { MyMatters, Post } from 'types';
|
||||||
import { sortByDate } from 'lib/utils';
|
import { sortByDate } from 'lib/utils';
|
||||||
|
import { cache } from 'react';
|
||||||
|
|
||||||
export const dataPath = 'content/posts';
|
export const dataPath = 'content/posts';
|
||||||
|
|
||||||
@ -74,7 +75,7 @@ export const allPostsPath = async () => {
|
|||||||
* @param slug
|
* @param slug
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const readSinglePost = async (slug: string) => {
|
export const readSinglePost = cache(async (slug: string) => {
|
||||||
const filename = path.join(`${dataPath}/${slug}.mdx`);
|
const filename = path.join(`${dataPath}/${slug}.mdx`);
|
||||||
return await fs.readFile(filename, { encoding: 'utf-8' });
|
return await fs.readFile(filename, { encoding: 'utf-8' });
|
||||||
};
|
});
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import { cache } from 'react';
|
||||||
import * as THREE from 'three';
|
import * as THREE from 'three';
|
||||||
|
|
||||||
export const sortByDate = (
|
export const sortByDate = (
|
||||||
@ -47,7 +48,7 @@ export type SingleToc = {
|
|||||||
* @param source
|
* @param source
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const generateToc = (source: string) => {
|
export const generateToc = cache((source: string) => {
|
||||||
const regex = /^#{2,3}(?!#)(.*)/gm;
|
const regex = /^#{2,3}(?!#)(.*)/gm;
|
||||||
|
|
||||||
let lastH2: SingleToc | null = null;
|
let lastH2: SingleToc | null = null;
|
||||||
@ -86,7 +87,7 @@ export const generateToc = (source: string) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return toc;
|
return toc;
|
||||||
};
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get mouse or touch position on screen.
|
* Get mouse or touch position on screen.
|
||||||
|
Reference in New Issue
Block a user