@devries-cameron20 / t3code-1 / commits / cba00ae197
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): highlighted code tokens compile without manual memoization (#20678)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 21:54 UTC cba00ae1971fb42e140527ce4755a7a01976cfbf
1 file changed +16 -10
apps/web/src/components/chat/HighlightedTokens.tsx
+16 -10
@@ -1,4 +1,4 @@
1 -import { use, useMemo, type CSSProperties } from "react";
1 +import { use, type CSSProperties } from "react";
2
3 import { resolveDiffThemeName } from "../../lib/diffRendering";
4 import type { EmbeddedScript } from "../../lib/embeddedScripts";
@@ -29,6 +29,20 @@ function syntaxTokenStyle(token: SyntaxToken): CSSProperties {
29 * Suspends while the grammar loads; wrap it in Suspense with the plain text as
30 * the fallback.
31 */
32 +function useHighlightedLines(
33 + highlighter: Awaited<ReturnType<typeof getSyntaxHighlighterPromise>>,
34 + code: string,
35 + language: string,
36 + embedded: ReadonlyArray<EmbeddedScript>,
37 + theme: "light" | "dark",
38 +) {
39 + "use memo";
40 + const themeName = resolveDiffThemeName(theme);
41 + const tokenize = (text: string, lang: string) =>
42 + highlighter.codeToTokens(text, { lang, theme: themeName }).tokens;
43 + return keyedLines(code, withEmbeddedScripts(code, tokenize(code, language), embedded, tokenize));
44 +}
45 +
46 export function HighlightedTokens({
47 code,
48 language,
@@ -44,15 +58,7 @@ export function HighlightedTokens({
58 }) {
59 const highlighter = use(getSyntaxHighlighterPromise(language));
60 for (const script of embedded) use(getSyntaxHighlighterPromise(script.language));
47 - const lines = useMemo(() => {
48 - const themeName = resolveDiffThemeName(theme);
49 - const tokenize = (text: string, lang: string) =>
50 - highlighter.codeToTokens(text, { lang, theme: themeName }).tokens;
51 - return keyedLines(
52 - code,
53 - withEmbeddedScripts(code, tokenize(code, language), embedded, tokenize),
54 - );
55 - }, [code, embedded, highlighter, language, theme]);
61 + const lines = useHighlightedLines(highlighter, code, language, embedded, theme);
62
63 return lines.map(({ key, tokens, ending }) => (
64 <span key={key}>