Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-03-22 - [Static Icon Imports Anti-Pattern]
**Learning:** Statically importing entire icon sets (e.g. `import { icons as materialIcons } from "@iconify-json/material-symbols"`) bundles MEGABYTES of unused JSON data into the client bundle, causing massive performance degradation.
**Action:** Always verify icon usage in Next.js/React apps. Prefer `@iconify/react` with string-based icon names (e.g. "mdi:home") which fetches icons on demand, or use a bundler plugin to tree-shake.
67 changes: 23 additions & 44 deletions client/src/components/base/IconifyIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,52 +1,31 @@
import { icons as entypoSocialIcons } from "@iconify-json/entypo-social";
import { icons as evaIcons } from "@iconify-json/eva";
import { icons as fa6Brands } from "@iconify-json/fa6-brands";
import { icons as flagIcons } from "@iconify-json/flag";
import { icons as icIcons } from "@iconify-json/ic";
import { icons as materialIcons } from "@iconify-json/material-symbols";
import { icons as materialLightIcons } from "@iconify-json/material-symbols-light";
import { icons as mdiIcons } from "@iconify-json/mdi";
import { icons as mdiLightIcons } from "@iconify-json/mdi-light";
import { icons as riIcons } from "@iconify-json/ri";
import { icons as twemojiIcons } from "@iconify-json/twemoji";
import { Icon, IconifyJSON, IconProps } from "@iconify/react";
import { getIconData } from "@iconify/utils";
import { Icon, IconProps } from "@iconify/react";
import { Box, BoxProps } from "@mui/material";

interface IconifyProps extends IconProps {
sx?: BoxProps["sx"];
// Use BoxProps<'svg'> to ensure event handlers (onError, etc.) are compatible with SVG elements
// intersect with IconProps specific properties (rotate, flip, etc.)
type IconSpecificProps = Omit<IconProps, keyof BoxProps<'svg'>>;

interface IconifyProps extends BoxProps<'svg'>, IconSpecificProps {
sx?: BoxProps['sx']; // Ensure sx is available (it is in BoxProps but good to be explicit if needed, though BoxProps<'svg'> has it)
flipOnRTL?: boolean;
icon: string;
}

const iconSets: Record<string, IconifyJSON> = {
"material-symbols": materialIcons,
"material-symbols-light": materialLightIcons,
twemoji: twemojiIcons,
eva: evaIcons,
ri: riIcons,
ic: icIcons,
flag: flagIcons,
"fa6-brands": fa6Brands,
"entypo-social": entypoSocialIcons,
mdi: mdiIcons,
"mdi-light": mdiLightIcons,
};

const iconData = (icon: string) => {
const [prefix, name] = icon.includes(":") ? icon.split(":") : ["", icon];

if (prefix && iconSets[prefix]) {
const data = getIconData(iconSets[prefix], name);
if (data) return data;
}

for (const [_, icons] of Object.entries(iconSets)) {
const data = getIconData(icons, name);
if (data) return data;
}
};

/**
* Optimized IconifyIcon component.
*
* Performance Improvement:
* Previously, this component statically imported entire icon sets from @iconify-json/*,
* which caused massive bundle bloat (loading thousands of unused icons).
*
* This refactored version passes the icon string (e.g., "mdi:home") directly to the
* @iconify/react Icon component, allowing it to fetch icons on demand.
*
* Benefits:
* - Significantly reduced initial bundle size.
* - Faster application load time.
* - Eliminated unused icon data from the build.
*/
const IconifyIcon = ({
icon,
flipOnRTL = false,
Expand All @@ -56,7 +35,7 @@ const IconifyIcon = ({
return (
<Box
component={icon ? Icon : "span"}
{...(icon ? { icon: iconData(icon), ssr: true } : ({} as any))}
{...(icon ? { icon } : {})}
className='iconify'
sx={[
flipOnRTL && {
Expand Down