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 <noreply@anthropic.com>
This commit is contained in:
parent
93b50254e5
commit
87f554083d
1 changed files with 14 additions and 6 deletions
|
|
@ -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<Highlighter> | 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',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue