From 87f554083dfdb56ea0c234054347e4e1c4d71934 Mon Sep 17 00:00:00 2001 From: Scrum4Me Agent <30029041+madhura68@users.noreply.github.com> Date: Wed, 13 May 2026 23:31:46 +0200 Subject: [PATCH] refactor(caddy): extract module-level highlighter singleton Replace inline createHighlighter() call with a module-level singleton so the Caddyfile grammar is parsed only once across requests. Add type Highlighter import for proper TypeScript typing. Co-Authored-By: Claude Sonnet 4.6 --- app/caddy/page.tsx | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/caddy/page.tsx b/app/caddy/page.tsx index 3e3e411..ff20c4b 100644 --- a/app/caddy/page.tsx +++ b/app/caddy/page.tsx @@ -1,6 +1,6 @@ import { redirect } from 'next/navigation' import Link from 'next/link' -import { createHighlighter } from 'shiki' +import { createHighlighter, type Highlighter } from 'shiki' import caddyfileGrammar from '@/lib/grammars/caddyfile.json' import { getCurrentUser } from '@/lib/session' import { execAgent } from '@/lib/agent-client' @@ -9,6 +9,18 @@ import CaddyView from './_components/caddy-view' export const dynamic = 'force-dynamic' +let highlighterPromise: Promise | null = null +function getHighlighter() { + if (!highlighterPromise) { + highlighterPromise = createHighlighter({ + themes: ['github-dark'], + // eslint-disable-next-line @typescript-eslint/no-explicit-any + langs: [caddyfileGrammar as any], + }) + } + return highlighterPromise +} + export default async function CaddyPage() { const user = await getCurrentUser() if (!user) redirect('/login') @@ -17,11 +29,7 @@ export default async function CaddyPage() { let configError: string | null = null try { const raw = await execAgent('caddy_show_config') - const highlighter = await createHighlighter({ - themes: ['github-dark'], - // eslint-disable-next-line @typescript-eslint/no-explicit-any - langs: [caddyfileGrammar as any], - }) + const highlighter = await getHighlighter() configHtml = highlighter.codeToHtml(raw || '# (empty)', { lang: 'caddyfile', theme: 'github-dark',