Skip to content

Commit

Permalink
perf: Add MDX caching function
Browse files Browse the repository at this point in the history
  • Loading branch information
gorzelinski committed Sep 29, 2024
1 parent eb6d3c6 commit 2ae77f0
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion scripts/mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { LINKS, Pages } from '@/constants'
import { Locale } from '@/i18n.config'
import { localizeFileName, localizePath } from '@/lib'
import { getMDXComponents } from '@/mdx-components'
import { unstable_cache } from 'next/cache'

type Frontmatter = {
slug: string
Expand Down Expand Up @@ -96,6 +97,11 @@ export async function getMDX<Type extends MDXTypes>(
}
}

export const getCachedMDX = unstable_cache(
async <Type extends MDXTypes>(page: Pages, slug: string, lang: Locale) =>
getMDX<Type>(page, slug, lang)
)

export async function getSlugs(
page: Extract<Pages, (typeof LINKS)['blog' | 'portfolio']>
) {
Expand Down Expand Up @@ -141,7 +147,7 @@ export async function getMDXes<Type extends MDXTypes>(
) {
const slugs = await getSlugs(page)
const mdxes = await Promise.all(
slugs.map((slug) => getMDX<Type>(page, slug, lang))
slugs.map((slug) => getCachedMDX<Type>(page, slug, lang))
)
const sorted = sort === 'none' ? mdxes : sortMDXes<Type>(mdxes, sort)
const filtered =
Expand Down

0 comments on commit 2ae77f0

Please sign in to comment.