Skip to content

fix(code-block): key the token cache on the full code and highlight unknown languages as text - #504

Open
HardMax71 wants to merge 2 commits into
vercel:mainfrom
HardMax71:fix/code-block-cache-key-and-unknown-lang
Open

HardMax71 wants to merge 2 commits into
vercel:mainfrom
HardMax71:fix/code-block-cache-key-and-unknown-lang

Conversation

@HardMax71

Copy link
Copy Markdown

Two fixes in the same cache code, one commit each:

  • The token cache key is now language:code. The old key already held the code length, so each streamed version of a growing block was already its own entry and the entry count does not change; the full string is small next to the tokens stored under it. The unbounded growth fix(elements): bound code block token cache with LRU eviction #454 addresses is untouched.
  • getHighlighter resolves a language that is neither in bundledLanguages nor special (text, plaintext, ...) to "text" before calling createHighlighter, so the existing "text" fallback in highlightCode applies and no rejected promise is cached.

Two new tests in __tests__/code-block.test.tsx, "does not show the tokens of other code with the same length and ends" and "highlights a language shiki does not bundle as plain text", fail on main and pass here; each also fails with only the other commit applied. The full @repo/elements suite passes (960 tests).

Closes #503

The token cache key was the language, the code length and the first and
last 100 characters, so two blocks with the same length and ends shared
one entry and the second block showed the first block's code. The key
already held the length, so every distinct code string is already its
own entry; using the full code only makes the key exact.
createHighlighter rejects a language that is not in the shiki bundle,
so the "text" fallback in highlightCode never ran: the rejected promise
was cached for that language, and every block in it stayed unhighlighted
and logged "Failed to highlight code". The language is now resolved to
"text" before the highlighter is created.
@vercel

vercel Bot commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

@HardMax71 is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CodeBlock: same-length blocks can show each other's code; unknown languages never highlight

1 participant