Skip to content

Commit

Permalink
Try fixing memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
mskelton committed Apr 2, 2024
1 parent 47b90f0 commit 3ac9d82
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/(main)/bytes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { cache } from "react"
import rehypeSlug from "rehype-slug"
import remarkGfm from "remark-gfm"
import remarkSmartypants from "remark-smartypants"
import { getHighlighter } from "shiki"
import { getHighlighter, Highlighter } from "shiki"
import prisma from "lib/prisma"
import MarkdownImage from "../../../components/markdown/MarkdownImage"
import MarkdownLink from "../../../components/markdown/MarkdownLink"
Expand All @@ -25,6 +25,8 @@ import { ByteMeta } from "./types"
// Revalidate the data at most every hour
export const revalidate = 3600

let highlighter: Highlighter | null

const loadLocalByteContent = async (id: string) => {
const dir = process.env.BYTES_DIR
if (!dir) return
Expand Down Expand Up @@ -58,7 +60,11 @@ export const getByte = cache(async (slug: string) => {
byte.content = (await loadLocalByteContent(byte.id)) ?? byte.content
}

const highlighter = await getHighlighter({ langs, themes: themes as any })
// Load the highlighter once and reuse it for all requests. Hopefully this
// fixes the memory leak issue with shiki and vscode-oniguruma.
if (!highlighter) {
highlighter = await getHighlighter({ langs, themes: themes as any })
}

const { content } = await compileMDX<ByteMeta>({
components: {
Expand Down

0 comments on commit 3ac9d82

Please sign in to comment.