Skip to content

fix(ui): register Geist font with canvas to fix badge width measurement#2196

Open
howwohmm wants to merge 1 commit intonpmx-dev:mainfrom
howwohmm:fix/badge-width
Open

fix(ui): register Geist font with canvas to fix badge width measurement#2196
howwohmm wants to merge 1 commit intonpmx-dev:mainfrom
howwohmm:fix/badge-width

Conversation

@howwohmm
Copy link

Summary

  • Register the Geist font with @napi-rs/canvas GlobalFonts so server-side text measurement uses the same font the SVG specifies for browser rendering
  • Add serverAssets config in Nitro to expose public/fonts/ to server-side code via useStorage
  • Font registration is lazy (once per process) and best-effort — falls back gracefully if the font file is unavailable

Root cause

The badge SVG specifies font-family="Geist, system-ui, ..." but @napi-rs/canvas was never given the Geist font file. In production, canvas measured text with a wider system fallback font, producing oversized width attributes on the SVG. The browser then rendered the text narrower (using Geist or system-ui), leaving visible whitespace gaps — especially noticeable for longer badge values.

Locally, the Geist font may be installed system-wide, which is why pnpm dev produced correct-looking badges.

Closes #2187

Test plan

  • Verify /api/registry/badge/engines/vitest no longer has excessive whitespace
  • Verify other badge types (version, license, downloads) render with correct widths
  • Verify style=shieldsio badges are unaffected
  • Verify badges still render correctly when canvas/font registration fails (graceful fallback)

🤖 Generated with Claude Code

The Geist font was not registered with @napi-rs/canvas, causing text
measurement to use a wider system fallback font in production. This
produced badges with excessive whitespace, especially for longer text.

Register the Geist font from server assets so canvas measures text
with the same font the SVG specifies for rendering.

Closes npmx-dev#2187

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Mar 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
npmx.dev Ready Ready Preview, Comment Mar 22, 2026 0:38am
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs.npmx.dev Ignored Ignored Preview Mar 22, 2026 0:38am
npmx-lunaria Ignored Ignored Mar 22, 2026 0:38am

Request Review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 22, 2026

📝 Walkthrough

Walkthrough

The pull request adds server-side font asset configuration and Geist font registration for badge generation. The nuxt.config.ts file now includes a nitro.serverAssets configuration to serve font files from ./public/fonts under the fonts asset group. The badge handler in server/api/registry/badge/[type]/[...pkg].get.ts introduces a module-level flag and async function to register the Geist font with @napi-rs/canvas on first use, retrieving the font from assets storage and suppressing registration errors silently.

Possibly related PRs

Suggested reviewers

  • ghostdevv
  • danielroe
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The pull request description clearly relates to the changeset, describing the font registration implementation and root cause of the badge width issue.
Linked Issues check ✅ Passed The pull request successfully addresses the core requirement from issue #2187 by registering the Geist font with @napi-rs/canvas and adding Nitro serverAssets configuration to expose fonts to server-side code.
Out of Scope Changes check ✅ Passed All changes are scoped to the linked issue requirements: Nitro configuration for font assets and canvas font registration in the badge API handler; no unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
server/api/registry/badge/[type]/[...pkg].get.ts (1)

55-73: Minor race condition on concurrent requests.

Multiple simultaneous requests before registration completes will all enter the try block and attempt registration. Whilst likely benign (redundant work rather than corruption), a Promise-based singleton pattern avoids repeated font loads.

🔧 Suggested fix using Promise singleton
-let fontRegistered = false
+let fontRegistrationPromise: Promise<void> | null = null

-async function registerGeistFont(): Promise<void> {
-  if (fontRegistered) return
+function registerGeistFont(): Promise<void> {
+  if (fontRegistrationPromise) return fontRegistrationPromise

-  try {
-    const fontStorage = useStorage('assets:fonts')
-    const fontData = await fontStorage.getItemRaw('Geist-Regular.ttf')
-
-    if (fontData) {
-      const buffer = fontData instanceof Buffer ? fontData : Buffer.from(fontData as ArrayBuffer)
-      GlobalFonts.register(buffer, 'Geist')
+  fontRegistrationPromise = (async () => {
+    try {
+      const fontStorage = useStorage('assets:fonts')
+      const fontData = await fontStorage.getItemRaw('Geist-Regular.ttf')
+
+      if (fontData) {
+        const buffer = fontData instanceof Buffer ? fontData : Buffer.from(fontData as ArrayBuffer)
+        GlobalFonts.register(buffer, 'Geist')
+      }
+    } catch {
+      // font registration is best-effort; fallback measurement will be used
     }
-  } catch {
-    // font registration is best-effort; fallback measurement will be used
-  }
+  })()

-  fontRegistered = true
+  return fontRegistrationPromise
 }

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9c55b5f7-3bc7-4932-908c-fe4e89a53437

📥 Commits

Reviewing files that changed from the base of the PR and between 7f2fc1a and 7190baf.

📒 Files selected for processing (2)
  • nuxt.config.ts
  • server/api/registry/badge/[type]/[...pkg].get.ts

@howwohmm howwohmm changed the title fix(badge): register Geist font with canvas to fix width measurement fix(ui): register Geist font with canvas to fix badge width measurement Mar 22, 2026
@codecov
Copy link

codecov bot commented Mar 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

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.

Custom badges width too wide

1 participant