An open source design system that's fully customizable and agent ready
TypeScript
fix(docsite): stop registering the preview theme's icons globally (#5540)
Xinzi Zhou445233f6a442
diff --git a/apps/docsite/src/__tests__/component-preview-theme.test.ts b/apps/docsite/src/__tests__/component-preview-theme.test.tsindex f17a309834..c4def4d79d 100644--- a/apps/docsite/src/__tests__/component-preview-theme.test.ts+++ b/apps/docsite/src/__tests__/component-preview-theme.test.ts@@ -1,19 +1,32 @@// Copyright (c) Meta Platforms, Inc. and affiliates.-import {readFileSync} from 'node:fs';-import {dirname, join} from 'node:path';+import {readdirSync, readFileSync} from 'node:fs';+import {dirname, join, relative} from 'node:path';import {fileURLToPath} from 'node:url';import {describe, expect, it} from 'vitest';-const componentDetailDir = join(- dirname(fileURLToPath(import.meta.url)),- '../components/component-detail',-);+const srcDir = join(dirname(fileURLToPath(import.meta.url)), '..');+const componentDetailDir = join(srcDir, 'components/component-detail');function readComponentDetailFile(name: string): string {return readFileSync(join(componentDetailDir, name), 'utf8');}+// Every hand-written source file in the docsite. `generated/` is skipped: it is+// machine-written data that embeds core's own docs (which mention the icon API+// in prose), not code the docsite runs.+function sourceFiles(dir: string = srcDir): string[] {+ return readdirSync(dir, {withFileTypes: true}).flatMap(entry => {+ const path = join(dir, entry.name);+ if (entry.isDirectory()) {+ return entry.name === 'generated' || entry.name === '__tests__'+ ? []+ : sourceFiles(path);+ }+ return /\.tsx?$/.test(entry.name) ? [path] : [];+ });+}+describe('component detail preview theming', () => {it('applies the neutral preview theme around preview containers', () => {expect(readComponentDetailFile('ComponentDetailClient.tsx')).toMatch(@@ -38,4 +51,18 @@ describe('component detail preview theming', () => {'Theme theme={neutralTheme}',);});++ it('lets <Theme> carry the preview icons instead of registering them globally', () => {+ // registerIcons() writes to a registry shared by the whole server process,+ // but on the client it only runs in the bundles that import the calling+ // module. Calling it from a route-specific module therefore hands SSR a set+ // of icons the client does not have, and every route that never loads that+ // module fails hydration with React #418. <Theme> already resolves a+ // theme's icons by name, so nothing here needs the global registry.+ const offenders = sourceFiles()+ .filter(file => /\bregisterIcons\s*\(/.test(readFileSync(file, 'utf8')))+ .map(file => relative(srcDir, file));++ expect(offenders).toEqual([]);+ });});diff --git a/apps/docsite/src/components/component-detail/ComponentPreviewTheme.tsx b/apps/docsite/src/components/component-detail/ComponentPreviewTheme.tsxindex b016681b10..25383ea9fb 100644--- a/apps/docsite/src/components/component-detail/ComponentPreviewTheme.tsx+++ b/apps/docsite/src/components/component-detail/ComponentPreviewTheme.tsx@@ -14,17 +14,19 @@import {type ReactNode} from 'react';import {Theme} from '@astryxdesign/core/theme';import {neutralTheme} from '@astryxdesign/theme-neutral/built';-import {registerIcons} from '@astryxdesign/core/Icon';import {useThemeMode} from '../../app/providers';-// Register theme icons at module load time so the globalIconRegistry is-// consistent between SSR and client hydration. Without this, the server's-// module-level registry may already have Lucide icons (from a warm process),-// while the client starts with default icons — causing an SVG strokeWidth-// mismatch for any Icon that renders before Theme's render-time registerIcons call.-if (neutralTheme.icons) {- registerIcons(neutralTheme.icons);-}+// The neutral theme's icons reach the previews through <Theme> alone: it calls+// registerTheme(theme) as it renders, and every Icon below resolves its+// semantic name against that theme (useThemeName -> getIcon(name, themeName)).+//+// Do NOT add a global icon registration here. That API writes to a process-wide+// registry, and this module is in the client bundle of the component-detail+// routes ONLY — while on the server one module registry is shared by every+// route. Registering here therefore gave the SSR pass of pages that never load+// this module (the home page, /blog, /templates, /docs/*) the Lucide icons+// while their client bundle still had the built-in defaults, and hydration+// failed with React #418 on those routes.export function ComponentPreviewTheme({children}: {children: ReactNode}) {const {mode} = useThemeMode();