Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/elements/src/code-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,16 @@ export const CodeBlockContent = ({
const [asyncTokens, setAsyncTokens] = useState<TokenizedCode | null>(null);
const asyncKeyRef = useRef({ code, language });

// Gate highlighting on mount so the first render is always `rawTokens`.
// `tokensCache` is module-level and lives for the whole server process, so once
// the server has highlighted a block, later SSR responses emit COLORED tokens —
// while every fresh client starts with a cold cache and its first (hydration)
// render emits plain `rawTokens`. Those two disagree, which React reports as a
// hydration mismatch. Forcing both to `rawTokens` initially makes them
// identical regardless of cache state; shiki is applied after mount.
const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);

// Invalidate stale async tokens synchronously during render
if (
asyncKeyRef.current.code !== code ||
Expand All @@ -416,7 +426,7 @@ export const CodeBlockContent = ({
};
}, [code, language]);

const tokenized = asyncTokens ?? syncTokens;
const tokenized = mounted ? (asyncTokens ?? syncTokens) : rawTokens;

return (
<div className="relative overflow-auto">
Expand Down