Skip to content

codeblock: replace prism-react-renderer with shiki#18194

Open
pettinarip wants to merge 1 commit into
devfrom
codeblock-shiki
Open

codeblock: replace prism-react-renderer with shiki#18194
pettinarip wants to merge 1 commit into
devfrom
codeblock-shiki

Conversation

@pettinarip
Copy link
Copy Markdown
Member

Summary

Replaces prism-react-renderer with shiki for code block syntax highlighting. The new Codeblock is an async server component; the client wrapper only owns the collapse + copy controls.

Why

  • Correctness across the languages we actually use. Our content has 13k+ Solidity blocks, 4.9k Python, 2.1k bash/shell, 1k Rust, plus JS/TS/TSX/JSX. prism-react-renderer (and JS-only alternatives like sugar-high) mis-tokenise everything outside the JS family on the tutorials and docs pages — exactly where highlighting matters most. Shiki ships 692 grammars and handles all of them.
  • Zero client JS. Highlighting runs server-side; the resulting HTML string is handed to a tiny "use client" wrapper via dangerouslySetInnerHTML. Grammars/themes never cross the RSC boundary, so the client bundle does not grow.
  • Build-time cost, not request-time. Cost is ~10–16 ms per block on cold call, amortised across statically-generated tutorial/docs routes — well under the noise floor next to MDX compilation.
  • Theme switching with no re-highlight. Shiki emits inline --shiki-light / --shiki-dark CSS variables; the dark theme is selected purely via the .dark ancestor.

Changes

  • New src/components/Codeblock/ (server Codeblock.tsx + client CodeblockClient.tsx) replaces the old single-file src/components/Codeblock.tsx.
  • src/lib/shiki.ts — singleton highlighter with a fixed lang/theme allowlist (one-light + one-dark-pro) to avoid lazy-import latency on cold starts.
  • src/styles/codeblock.css — token styles + dark-mode variable swap, imported once from global.css.

Test plan

  • Tutorials render correctly in light + dark mode (/developers/tutorials/uniswap-v2-annotated-code — 149 blocks, good stress test)
  • Solidity, Python, bash, Rust blocks tokenise (not just JS)
  • Copy button + collapse still work
  • No shiki / codeToHtml / @shikijs symbols leak into .next/static/chunks/*.js

@pettinarip pettinarip requested a review from wackerow as a code owner May 14, 2026 12:57
@netlify
Copy link
Copy Markdown

netlify Bot commented May 14, 2026

Deploy Preview for ethereumorg ready!

Name Link
🔨 Latest commit 365734b
🔍 Latest deploy log https://app.netlify.com/projects/ethereumorg/deploys/6a05c6b917157a000725ba6f
😎 Deploy Preview https://deploy-preview-18194.ethereum.it
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
7 paths audited
Performance: 72 (🟢 up 7 from production)
Accessibility: 96 (no change from production)
Best Practices: 100 (no change from production)
SEO: 98 (🔴 down 1 from production)
PWA: 59 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added dependencies 📦 Changes related to project dependencies tooling 🔧 Changes related to tooling of the project labels May 14, 2026
Copy link
Copy Markdown
Member

@wackerow wackerow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple non-blocking comments, but this looks great to me @pettinarip, thanks! This paired with #18195 will be a nice polish to all the codeblocks.

Comment on lines +60 to +61
const shouldShowCopyWidget =
resolvedLang !== "text" && COPY_WIDGET_LANGS.has(resolvedLang)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is parity with existing, but wondering if we should just make the copy button available for all of these -- especially with this design, it doesn't really get in the way much.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious if there is a reason for not placing this directly inside index.tsx (vs the index.ts exporter)?

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

Labels

dependencies 📦 Changes related to project dependencies tooling 🔧 Changes related to tooling of the project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants